SCJP Java Collections Framework 4 — Questions and Answers
Question 1: What happens when you call `TreeMap.subMap(fromKey, toKey)` and then try to insert a key outside that range into the returned view?
- The key is silently ignored
- An IllegalArgumentException is thrown (Correct answer)
- A ConcurrentModificationException is thrown
- The key is inserted into the backing TreeMap directly
Correct answer: An IllegalArgumentException is thrown
Inserting a key outside the range of a subMap view throws IllegalArgumentException because the view enforces its boundary constraints.
Question 2: Which statement is true about `LinkedHashMap`?
- It maintains elements in natural sorted order
- It maintains insertion order by default (Correct answer)
- It does not allow null keys
- It is synchronized by default
Correct answer: It maintains insertion order by default
LinkedHashMap maintains insertion order (or optionally access order) by using a doubly-linked list alongside the hash table.
Question 3: Given `List<String> list = Arrays.asList("a", "b", "c");`, what happens when you call `list.add("d")`?
- "d" is appended to the list
- A NullPointerException is thrown
- An UnsupportedOperationException is thrown (Correct answer)
- A ConcurrentModificationException is thrown
Correct answer: An UnsupportedOperationException is thrown
Arrays.asList() returns a fixed-size list backed by the array; structural modifications like add() throw UnsupportedOperationException.
Question 4: Which collection class is best suited for implementing a LIFO (Last-In-First-Out) stack in modern Java?
- java.util.Stack
- java.util.LinkedList used as a Deque
- java.util.ArrayDeque (Correct answer)
- java.util.PriorityQueue
Correct answer: java.util.ArrayDeque
ArrayDeque is the preferred stack implementation because it is faster than Stack (which is synchronized) and more memory-efficient than LinkedList.
Question 5: What is the contract between `equals()` and `hashCode()` that must be satisfied for objects used as HashMap keys?
- If a.equals(b) is true, a.hashCode() must equal b.hashCode() (Correct answer)
- If a.hashCode() == b.hashCode(), then a.equals(b) must be true
- hashCode() must return a unique value for each object
- equals() must return true only when objects are the same reference
Correct answer: If a.equals(b) is true, a.hashCode() must equal b.hashCode()
The contract requires that equal objects must have the same hash code; unequal objects may share hash codes (collisions are allowed).
Question 6: Which interface does `PriorityQueue` implement that defines its ordering behavior?
- Comparable
- Comparator
- Queue (Correct answer)
- SortedSet
Correct answer: Queue
PriorityQueue implements the Queue interface; ordering is determined by the natural order of elements or a Comparator passed at construction.
Question 7: When iterating over a `HashMap` with an `Iterator` and you remove an element using `map.remove(key)` (not `iterator.remove()`), what occurs?
- The element is removed and iteration continues normally
- A ConcurrentModificationException is thrown on the next iterator call (Correct answer)
- The iterator skips the next element silently
- A NullPointerException is thrown immediately
Correct answer: A ConcurrentModificationException is thrown on the next iterator call
Structurally modifying a HashMap while iterating it via a fail-fast iterator (without using iterator.remove()) causes ConcurrentModificationException.
What happens when you call `TreeMap.subMap(fromKey, toKey)` and then try to insert a key outside that range into the returned view?