Showing 11–20 of 100 questions
in Collections Framework
Which collection interface guarantees that elements are stored in a sorted order with no duplicates?
A
List
B
Queue
C
SortedSet
D
Collection
Correct Answer:
C. SortedSet
EXPLANATION
SortedSet interface guarantees sorted order and no duplicates. TreeSet is the primary implementation of SortedSet.
What is the main advantage of EnumSet over HashSet when working with Enum constants?
A
EnumSet is thread-safe
B
EnumSet uses bit vectors internally for better performance
C
EnumSet allows null elements
D
EnumSet maintains insertion order
Correct Answer:
B. EnumSet uses bit vectors internally for better performance
EXPLANATION
EnumSet is specialized for Enum constants and uses bit vectors internally, providing O(1) operations with minimal memory overhead.
Which method in Collections can create a thread-safe Map from a non-thread-safe Map?
A
Collections.synchronizedMap()
B
Collections.synchronizedList()
C
new ConcurrentHashMap()
D
Collections.unmodifiableMap()
Correct Answer:
A. Collections.synchronizedMap()
EXPLANATION
Collections.synchronizedMap() wraps a Map with synchronized access. However, iteration still requires external synchronization.
Consider a TreeMap with Integer keys. What will headMap(10) return?
A
All entries with keys greater than 10
B
All entries with keys less than 10
C
All entries with keys less than or equal to 10
D
All entries in reverse order
Correct Answer:
B. All entries with keys less than 10
EXPLANATION
TreeMap.headMap(10) returns a view of all entries with keys strictly less than 10. It's exclusive of the provided key.
What is the purpose of the fail-fast iterator in Collections?
A
To speed up iteration
B
To detect concurrent modifications during iteration
C
To remove elements faster
D
To sort elements during iteration
Correct Answer:
B. To detect concurrent modifications during iteration
EXPLANATION
Fail-fast iterators throw ConcurrentModificationException if the collection is modified during iteration (except via iterator.remove()).
Which collection implementation is best for frequent insertions and deletions at both ends?
A
ArrayList
B
LinkedList
C
Vector
D
Stack
Correct Answer:
B. LinkedList
EXPLANATION
LinkedList provides O(1) operations for adding/removing at both ends via addFirst(), addLast(), removeFirst(), removeLast().
What happens when you add a null key to a HashMap?
A
NullPointerException is thrown
B
The null key is stored at index 0
C
The null key is ignored
D
The insertion fails silently
Correct Answer:
B. The null key is stored at index 0
EXPLANATION
HashMap allows one null key which is handled specially and stored at index 0. TreeMap does not allow null keys.
Which method returns true if the map contains the specified value?
A
containsKey(Object key)
B
containsValue(Object value)
C
contains(Object value)
D
hasValue(Object value)
Correct Answer:
B. containsValue(Object value)
EXPLANATION
The containsValue() method checks if the map contains a specified value. containsKey() checks for keys.
What is the initial capacity of a HashMap in Java?
EXPLANATION
HashMap has an initial capacity of 16 and doubles its capacity when load factor (0.75) is reached.
Which collection allows duplicate keys in Java?
A
HashMap
B
HashSet
C
TreeSet
D
None of these
Correct Answer:
D. None of these
EXPLANATION
No collection allows duplicate keys. When a duplicate key is added to a Map, it overwrites the previous value. HashSet and TreeSet don't allow duplicates.