1Z0-819 Collections Framework 3 β Questions and Answers
Question 1: Which collection type in Java guarantees that no duplicate elements are stored AND maintains elements in sorted order?
- LinkedHashSet
- HashSet
- TreeSet (Correct answer)
- ArrayDeque
Correct answer: TreeSet
`TreeSet` implements `NavigableSet` backed by a `TreeMap`, which stores elements with no duplicates in their natural or comparator-defined sorted order.
Question 2: What does `Collections.disjoint(Collection<?> c1, Collection<?> c2)` return when the two collections share at least one common element?
- true
- false (Correct answer)
- throws IllegalArgumentException
- 1
Correct answer: false
`Collections.disjoint()` returns `true` if the collections have NO elements in common, and `false` if they share at least one element.
Question 3: Which method of `PriorityQueue` removes and returns the head element, or returns null if the queue is empty?
- remove()
- poll() (Correct answer)
- peek()
- pop()
Correct answer: poll()
`poll()` retrieves and removes the head, returning null if empty, while `remove()` also removes the head but throws `NoSuchElementException` if empty.
Question 4: What is the time complexity of `HashMap.get()` in the average case?
- O(log n)
- O(n)
- O(1) (Correct answer)
- O(n log n)
Correct answer: O(1)
`HashMap.get()` has O(1) average-case performance because it computes the hash and directly indexes into the backing array.
Question 5: Which statement about `CopyOnWriteArrayList` is TRUE?
- It throws ConcurrentModificationException during iteration
- It is synchronized and blocks writes during reads
- Iterators reflect changes made after the iterator was created
- Writes create a fresh copy of the underlying array (Correct answer)
Correct answer: Writes create a fresh copy of the underlying array
Every mutating operation on `CopyOnWriteArrayList` creates a new copy of the array, making iterators snapshot-safe but memory-intensive.
Question 6: Which interface does `ArrayDeque` implement that `ArrayList` does NOT?
- List
- Iterable
- Deque (Correct answer)
- Serializable
Correct answer: Deque
`ArrayDeque` implements `Deque` (double-ended queue) allowing efficient insertion and removal at both ends, which `ArrayList` does not support via that interface.
Question 7: After calling `List.of(1, 2, 3)`, which operation will succeed without throwing an exception?
- list.add(4)
- list.set(0, 10)
- list.contains(2) (Correct answer)
- list.remove(0)
Correct answer: list.contains(2)
`List.of()` returns an unmodifiable list, so structural mutations like `add`, `set`, and `remove` throw `UnsupportedOperationException`, but read operations like `contains()` work fine.
Which collection type in Java guarantees that no duplicate elements are stored AND maintains elements in sorted order?