Question 268 of 481
1Z0-811 Java Basics and Syntax Practice Question
A web application uses a HashMap to cache user session data. The keys are String objects representing session IDs, and values are Session objects. After some time, the cache memory usage grows excessively, causing OutOfMemoryError. The cache is never cleared. The team decides to implement a cache eviction policy. Which approach is best suited for this scenario in terms of Java standard library?
⚠ Common exam trap
Oracle often tests the distinction between data structures that provide automatic eviction (LinkedHashMap) versus those that only manage references (WeakHashMap) or ordering (TreeMap), leading candidates to mistakenly choose WeakHashMap because they confuse weak references with cache eviction.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Use LinkedHashMap with removeEldestEntry method.
LinkedHashMap provides a straightforward way to implement a cache eviction policy by overriding the `removeEldestEntry` method. This method is automatically called by `put` and `putAll` to decide whether to remove the eldest entry, allowing you to limit the cache size and prevent OutOfMemoryError without manual eviction logic.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Use WeakHashMap instead of HashMap.
Why it's wrong here
WeakHashMap evicts based on weak references, not time or size.
- ✗
Wrap the HashMap with Collections.synchronizedMap.
Why it's wrong here
Does not address memory issue.
- ✓
Use LinkedHashMap with removeEldestEntry method.
Why this is correct
LinkedHashMap can implement LRU cache easily.
- ✗
Use TreeMap with a custom comparator.
Why it's wrong here
TreeMap does not provide eviction.
About these practice questions
Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →
Last reviewed: Jun 30, 2026
This 1Z0-811 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-811 exam.
Question Discussion
Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.
Sign in to join the discussion.