SCJP Java Collections Framework 5 — Questions and Answers
Question 1: What is the difference between `HashSet` and `LinkedHashSet`?
- LinkedHashSet is sorted; HashSet is not
- LinkedHashSet maintains insertion order; HashSet does not (Correct answer)
- HashSet allows duplicates; LinkedHashSet does not
- LinkedHashSet is synchronized; HashSet is not
Correct answer: LinkedHashSet maintains insertion order; HashSet does not
LinkedHashSet uses a linked list internally to maintain the order in which elements were inserted, while HashSet has no ordering guarantee.
Question 2: Which method of the `Collections` class returns an unmodifiable view of a specified list?
- Collections.immutableList(list)
- Collections.unmodifiableList(list) (Correct answer)
- Collections.readOnlyList(list)
- Collections.frozenList(list)
Correct answer: Collections.unmodifiableList(list)
Collections.unmodifiableList() wraps a list in a view that throws UnsupportedOperationException on any mutating operation.
Question 3: What does the `NavigableMap` interface add over `SortedMap`?
- Thread-safety guarantees
- Methods like floorKey(), ceilingKey(), higherKey(), and lowerKey() (Correct answer)
- Support for null keys
- Automatic resizing
Correct answer: Methods like floorKey(), ceilingKey(), higherKey(), and lowerKey()
NavigableMap extends SortedMap with navigation methods that return the closest matches for given search targets, such as floor, ceiling, higher, and lower.
Question 4: Which of the following is NOT a characteristic of `ConcurrentHashMap`?
- It allows concurrent reads without locking
- It uses segment-level locking (or node-level in Java 8+)
- It allows null keys (Correct answer)
- It is thread-safe for updates
Correct answer: It allows null keys
ConcurrentHashMap does not permit null keys or null values, unlike HashMap which allows one null key.
Question 5: What is the time complexity of `contains()` on a `HashSet` in the average case?
- O(n)
- O(log n)
- O(1) (Correct answer)
- O(n log n)
Correct answer: O(1)
HashSet.contains() is O(1) on average because it computes the hash code to directly locate the bucket, then checks equality.
Question 6: Given `TreeSet<Integer> ts = new TreeSet<>(); ts.add(5); ts.add(1); ts.add(3);`, what does `ts.first()` return?
- 5
- 1 (Correct answer)
- 3
- A NoSuchElementException
Correct answer: 1
TreeSet stores elements in ascending natural order, so first() returns the smallest element, which is 1.
Question 7: Which statement accurately describes `Collections.sort()` for a `List<T>` where T does not implement `Comparable`?
- The list is sorted using the elements' hashCode values
- A ClassCastException is thrown at runtime (Correct answer)
- The list remains in its original order
- A compilation error occurs
Correct answer: A ClassCastException is thrown at runtime
If T does not implement Comparable and no Comparator is supplied, Collections.sort() throws a ClassCastException at runtime when it tries to cast elements to Comparable.
What is the difference between `HashSet` and `LinkedHashSet`?