1Z0-819 Collections Framework 2 — Questions and Answers
Question 1: Which method in the `List` interface returns an unmodifiable view of the list in Java 11?
- List.copyOf(list)
- Collections.unmodifiableList(list) (Correct answer)
- List.of(list.toArray())
- list.asUnmodifiable()
Correct answer: Collections.unmodifiableList(list)
`Collections.unmodifiableList()` wraps an existing list in an unmodifiable view, while `List.copyOf()` creates a new independent unmodifiable copy.
Question 2: What happens when you call `Map.entry(key, value)` in Java 11 and then try to call `setValue()` on the returned entry?
- It updates the map entry silently
- It throws UnsupportedOperationException (Correct answer)
- It returns null
- It throws NullPointerException
Correct answer: It throws UnsupportedOperationException
`Map.entry()` returns an immutable `Map.Entry`, so calling `setValue()` throws `UnsupportedOperationException`.
Question 3: Which `NavigableMap` method returns the greatest key strictly less than the given key, or null if there is no such key?
- floorKey()
- lowerKey() (Correct answer)
- ceilingKey()
- headKey()
Correct answer: lowerKey()
`lowerKey(k)` returns the greatest key strictly less than `k`, while `floorKey(k)` returns the greatest key less than or equal to `k`.
Question 4: Which of the following correctly describes the behavior of `LinkedHashMap` regarding iteration order?
- Iterates in sorted key order
- Iterates in insertion order by default (Correct answer)
- Iterates in random order
- Iterates in reverse insertion order
Correct answer: Iterates in insertion order by default
`LinkedHashMap` maintains a doubly-linked list to preserve insertion order (or access order if constructed with `accessOrder=true`).
Question 5: What is the return type of `Collections.frequency(Collection<?> c, Object o)`?
- long
- int (Correct answer)
- double
- Integer
Correct answer: int
`Collections.frequency()` returns a primitive `int` representing the number of times the specified element appears in the collection.
Question 6: Which `Deque` method retrieves but does NOT remove the head of the queue, and returns null if the deque is empty?
- getFirst()
- peek() (Correct answer)
- element()
- peekFirst()
Correct answer: peek()
Both `peek()` and `peekFirst()` return null for an empty deque; `peek()` is the `Queue`-interface method equivalent to `peekFirst()` in `Deque`.
Question 7: When iterating over a `HashSet` and calling `iterator.remove()`, what is the result?
- Throws ConcurrentModificationException
- Removes the element safely without ConcurrentModificationException (Correct answer)
- Does nothing because HashSet is unordered
- Throws UnsupportedOperationException
Correct answer: Removes the element safely without ConcurrentModificationException
Using the iterator's own `remove()` method is the safe way to remove elements during iteration and does not throw `ConcurrentModificationException`.
Which method in the `List` interface returns an unmodifiable view of the list in Java 11?