Showing 91–100 of 100 questions
in Collections Framework
What does the Comparable interface do in Collections Framework?
A
It defines comparison logic for sorting
B
It creates copies of objects
C
It manages memory allocation
D
It handles serialization
Correct Answer:
A. It defines comparison logic for sorting
EXPLANATION
Comparable interface provides compareTo() method to define natural ordering for objects. Used by TreeSet and TreeMap for sorting.
Which of the following is thread-safe?
A
HashMap
B
TreeMap
C
ConcurrentHashMap
D
LinkedHashMap
Correct Answer:
C. ConcurrentHashMap
EXPLANATION
ConcurrentHashMap is thread-safe and uses bucket-level locking. HashMap, TreeMap, and LinkedHashMap are not thread-safe.
What is the time complexity of add() operation in TreeSet?
A
O(1)
B
O(log n)
C
O(n)
D
O(n log n)
Correct Answer:
B. O(log n)
EXPLANATION
TreeSet is backed by a TreeMap which uses Red-Black tree. Add operation requires O(log n) time for tree balancing.
Which collection does NOT allow duplicate elements?
A
ArrayList
B
HashSet
C
LinkedList
D
Vector
Correct Answer:
B. HashSet
EXPLANATION
Set interface implementations like HashSet do not allow duplicates. Lists (ArrayList, LinkedList, Vector) allow duplicates.
What is the load factor in HashMap? What is its default value?
A
Ratio of entries to capacity; default is 0.5
B
Ratio of entries to capacity; default is 0.75
C
Number of collisions; default is 0.75
D
Hash code value; default is 0.5
Correct Answer:
B. Ratio of entries to capacity; default is 0.75
EXPLANATION
Load factor is the ratio of size to capacity. Default load factor in HashMap is 0.75, which provides good balance between time and space complexity.
Which method is used to remove an element from ArrayList while iterating?
A
Using remove() directly in loop
B
Using Iterator.remove()
C
Using Collections.remove()
D
Using System.arraycopy()
Correct Answer:
B. Using Iterator.remove()
EXPLANATION
Using remove() directly in loop causes ConcurrentModificationException. Iterator.remove() is the safe way to remove elements while iterating.
What is the difference between ArrayList and LinkedList?
A
ArrayList uses array, LinkedList uses linked list; ArrayList is faster for random access
B
LinkedList uses array, ArrayList uses linked list
C
Both use arrays internally
D
ArrayList maintains insertion order, LinkedList doesn't
Correct Answer:
A. ArrayList uses array, LinkedList uses linked list; ArrayList is faster for random access
EXPLANATION
ArrayList is backed by a resizable array providing O(1) random access. LinkedList uses doubly-linked list providing O(1) insertion/deletion at ends but O(n) for random access.
Which of the following collections maintains insertion order?
A
HashSet
B
TreeSet
C
LinkedHashSet
D
HashMap
Correct Answer:
C. LinkedHashSet
EXPLANATION
LinkedHashSet maintains insertion order using doubly-linked list. HashSet doesn't maintain order, TreeSet maintains sorted order, HashMap doesn't maintain insertion order.
What is the time complexity of get() method in HashMap?
A
O(n)
B
O(log n)
C
O(1) average case
D
O(n log n)
Correct Answer:
C. O(1) average case
EXPLANATION
HashMap provides O(1) average time complexity for get() operation. In worst case with hash collisions, it can be O(n).
Which interface does HashMap implement in Java Collections Framework?
A
Map interface
B
Collection interface
C
List interface
D
Set interface
Correct Answer:
A. Map interface
EXPLANATION
HashMap implements the Map interface which stores key-value pairs. It does not implement Collection, List, or Set interfaces directly.