Collections Framework Flashcards
7 cards from real 1Z0-819 practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Collections Framework flashcards as text
What is the behavior of `TreeMap.subMap(fromKey, toKey)` with respect to the boundary keys?
Answer: Inclusive on fromKey, exclusive on toKey
`TreeMap.subMap(fromKey, toKey)` returns a view with `fromKey` inclusive and `toKey` exclusive, matching the convention of standard range methods.
Which class provides a thread-safe variant of `HashMap` without synchronizing the entire map?
Answer: ConcurrentHashMap
`ConcurrentHashMap` uses segment-level or bucket-level locking to allow concurrent reads and partial concurrent writes without locking the full map.
Which `Map` method was added in Java 8 and computes a new value only if the key is absent?
Answer: computeIfAbsent()
`computeIfAbsent(key, mappingFunction)` invokes the function to compute a value only when the key is not already present, and stores the result.
What exception is thrown when you attempt to add a null element to a `TreeSet`?
Answer: NullPointerException
`TreeSet` uses natural ordering or a `Comparator`, and comparing `null` causes a `NullPointerException` when the first comparison is attempted.
Which of the following collection factory methods allows duplicate elements?
Answer: List.of(1, 1)
`List.of()` permits duplicate elements, whereas `Set.of()` and `Map.of()` with duplicate keys/elements throw `IllegalArgumentException` at construction time.
When using `Collections.sort(List list)`, what must type `T` satisfy?
Answer: T must implement Comparable
`Collections.sort()` requires elements to implement `Comparable` so their natural ordering can be used; otherwise, a `ClassCastException` is thrown at runtime.
Which `Queue` method adds an element and throws an exception if capacity is exceeded, rather than returning false?
Answer: add()
`add()` throws `IllegalStateException` when the queue is full, while `offer()` returns `false` on failure and is preferred for capacity-constrained queues.