1Z0-819 Collections Framework 5 — Questions and Answers
Question 1: What is the default initial capacity of a `HashMap` when constructed with no arguments?
- 8
- 10
- 16 (Correct answer)
- 32
Correct answer: 16
The default initial capacity of `HashMap` is 16, and it resizes by doubling when the load factor threshold (default 0.75) is exceeded.
Question 2: Which statement is TRUE about `EnumSet`?
- It can hold null elements
- It stores elements in insertion order
- It is backed by a bit vector for high performance (Correct answer)
- It allows non-enum elements via generics
Correct answer: It is backed by a bit vector for high performance
`EnumSet` is implemented as a bit vector (one or two `long` values), making it extremely fast and memory-efficient for enum-typed elements.
Question 3: What does the `Map.merge(key, value, remappingFunction)` method do if the key already exists with a non-null value?
- Replaces with the new value unconditionally
- Applies the remapping function to the old and new values (Correct answer)
- Throws an IllegalStateException
- Ignores the new value and keeps the old one
Correct answer: Applies the remapping function to the old and new values
`merge()` calls `remappingFunction.apply(oldValue, newValue)` when the key exists; if the function returns null, the key is removed.
Question 4: Which of the following correctly uses `Comparator.comparing()` to sort a list of strings by their length?
- Comparator.comparing(String::length) (Correct answer)
- Comparator.compare(String::length)
- Comparator.naturalOrder(String::length)
- new Comparator<>(String::length)
Correct answer: Comparator.comparing(String::length)
`Comparator.comparing(String::length)` creates a `Comparator<String>` that extracts the length as the sort key using a method reference.
Question 5: After calling `iterator.next()` on a `ListIterator`, which method moves the cursor backward?
- iterator.back()
- iterator.previous() (Correct answer)
- iterator.retreat()
- iterator.prior()
Correct answer: iterator.previous()
`ListIterator.previous()` returns the previous element and moves the cursor backward, enabling bidirectional traversal of a list.
Question 6: Which class would you use to maintain a fixed-size priority queue that automatically discards the lowest-priority elements when full?
- LinkedList
- ArrayDeque
- PriorityQueue (with custom size management) (Correct answer)
- TreeSet
Correct answer: PriorityQueue (with custom size management)
Java's `PriorityQueue` is unbounded, so maintaining a fixed-size bounded priority queue requires manual size checking and removal of the lowest-priority element when the limit is exceeded.
Question 7: What is the output of `Collections.nCopies(3, "Java")` when its size is queried?
- 1
- 3 (Correct answer)
- 0
- Throws UnsupportedOperationException
Correct answer: 3
`Collections.nCopies(3, "Java")` returns an immutable `List` containing 3 references to the string "Java", so its size is 3.
What is the default initial capacity of a `HashMap` when constructed with no arguments?