What is the output of LinkedHashMap iteration in the following code?
LinkedHashMap map = new LinkedHashMap();
map.put("A", 1);
map.put("B", 2);
map.put("C", 3);
for(String key : map.keySet()) System.out.print(key);
AABC
BCBA
CRandom order
DBCA
Correct Answer:
A. ABC
EXPLANATION
LinkedHashMap maintains insertion order. Elements are iterated in the order they were inserted.
Which collection class is synchronized and legacy?
AArrayList
BHashSet
CVector
DHashMap
Correct Answer:
C. Vector
EXPLANATION
Vector is a legacy synchronized collection similar to ArrayList. It is thread-safe but slower. ArrayList is preferred with Collections.synchronizedList() for synchronization.
What is the worst-case time complexity of HashMap.get() when hash collisions occur?
AO(1)
BO(log n)
CO(n)
DO(n log n)
Correct Answer:
C. O(n)
EXPLANATION
In worst case with all collisions stored in linked list, get() becomes O(n). From Java 8, if collisions exceed threshold, linked list converts to balanced tree, making it O(log n).