Java Collections Frame Work MCQs

1 min read

The Java Collections Framework provides a unified architecture consisting of interfaces (such as List, Set, and Map) and implementation classes (like ArrayList and HashMap) to store and manipulate object groups efficiently. It standardizes data structures, reducing programming effort while enhancing performance through optimized algorithms. Key interfaces include Collection and Map, which support specialized operations like sorting, searching, and hashing. Understanding internal complexities and behavioral differences between interfaces is vital for writing scalable code and clearing technical interviews.

1. Which of these interfaces is the root of the collection hierarchy?

a) Collection
b) List
c) Root
d) Iterable
Correct Answer: a) Collection
Explanation:
The Collection interface is the root of the collection hierarchy, though it extends Iterable.

2. Which of these interfaces does the Map interface extend?

a) Collection
b) Iterable
c) Cloneable
d) None of the above
Correct Answer: d) None of the above
Explanation:
The Map interface does not extend the Collection interface.

3. Which class is an implementation of a dynamic array?

a) LinkedList
b) ArrayList
c) HashSet
d) PriorityQueue
Correct Answer: b) ArrayList
Explanation:
ArrayList is a resizable-array implementation of the List interface.

4. What is the default initial capacity of an ArrayList in Java?

a) 0
b) 10
c) 16
d) 20
Correct Answer: b) 10
Explanation:
The default initial capacity of an ArrayList is 10.

5. Which collection does not allow duplicate elements?

a) List
b) Queue
c) Set
d) Deque
Correct Answer: c) Set
Explanation:
A Set is a collection that contains no duplicate elements.

6. Which of these classes implements a doubly linked list?

a) ArrayList
b) LinkedList
c) Vector
d) Stack
Correct Answer: b) LinkedList
Explanation:
LinkedList implements both the List and Deque interfaces and is internally backed by a doubly linked list.

7. Which interface provides methods for a First-In-First-Out (FIFO) collection?

a) Set
b) Map
c) Queue
d) Stack
Correct Answer: c) Queue
Explanation:
The Queue interface represents a collection designed for holding elements prior to processing in a FIFO manner.

8. Which Collection implementation is synchronized and thread-safe by default?

a) ArrayList
b) Vector
c) LinkedList
d) HashSet
Correct Answer: b) Vector
Explanation:
Vector is a legacy class from Java 1.0 and is synchronized.

9. What happens if you try to add a duplicate key to a HashMap?

a) Throws an Exception
b) The map remains unchanged
c) The new value replaces the old value
d) The old value is moved to a new key
Correct Answer: c) The new value replaces the old value
Explanation:
If a duplicate key is added, the HashMap replaces the old value associated with the key with the new value.

10. Which of the following guarantees O(1) time complexity for basic operations?

a) TreeSet
b) LinkedList
c) HashSet
d) ArrayList
Correct Answer: c) HashSet
Explanation:
HashSet offers constant time performance for basic operations, provided the hash function disperses elements properly.

11. Which Map implementation maintains the insertion order of its elements?

a) HashMap
b) TreeMap
c) LinkedHashMap
d) IdentityHashMap
Correct Answer: c) LinkedHashMap
Explanation:
LinkedHashMap uses a doubly-linked list running through all its entries to maintain insertion order.

12. Which Map implementation sorts its keys according to their natural ordering?

a) LinkedHashMap
b) TreeMap
c) HashMap
d) WeakHashMap
Correct Answer: b) TreeMap
Explanation:
TreeMap is a Red-Black tree based NavigableMap implementation that sorts its keys.

13. How many null keys can a HashMap contain?

a) 0
b) 1
c) Infinite
d) Depends on the capacity
Correct Answer: b) 1
Explanation:
HashMap permits exactly one null key and multiple null values.

14. How many null keys can a TreeMap contain if it uses natural ordering?

a) 0
b) 1
c) Infinite
d) Depends on capacity
Correct Answer: a) 0
Explanation:
TreeMap using natural ordering throws a NullPointerException if you attempt to add a null key.

15. Which iterator allows traversing a List in both forward and backward directions?

a) Iterator
b) ListIterator
c) Enumeration
d) Spliterator
Correct Answer: b) ListIterator
Explanation:
ListIterator allows traversing a list in both directions and modifying it during iteration.

16. What exception is thrown if a collection is modified during iteration with a fail-fast Iterator?

a) IllegalArgumentException
b) ConcurrentModificationException
c) UnsupportedOperationException
d) IllegalStateException
Correct Answer: b) ConcurrentModificationException
Explanation:
Fail-fast iterators throw ConcurrentModificationException if modified structurally during iteration.

17. Which legacy collection class is used to implement a LIFO stack?

a) Vector
b) Queue
c) Stack
d) LinkedList
Correct Answer: c) Stack
Explanation:
The Stack class represents a last-in-first-out (LIFO) stack of objects and extends Vector.

18. Which method is used in Collections to process elements in parallel in Java 8?

a) parallelStream()
b) stream()
c) iterator()
d) spliterator()
Correct Answer: a) parallelStream()
Explanation:
The parallelStream() method returns a possibly parallel Stream with the collection as its source.

19. What is the time complexity of the get() method in an ArrayList?

a) O(n)
b) O(log n)
c) O(1)
d) O(n log n)
Correct Answer: c) O(1)
Explanation:
ArrayList provides constant time O(1) performance for retrieving an element using its index.

20. What is the time complexity of the get() method in a LinkedList?

a) O(1)
b) O(n)
c) O(log n)
d) O(n^2)
Correct Answer: b) O(n)
Explanation:
Retrieving an element by index in a LinkedList takes O(n) time as it must traverse the list.

21. Which of these collections uses a Red-Black tree data structure?

a) HashMap
b) TreeSet
c) HashSet
d) LinkedHashMap
Correct Answer: b) TreeSet
Explanation:
TreeSet and TreeMap are implemented using a Red-Black tree structure to keep elements sorted.

22. Which interface should be implemented to define the natural ordering of a class?

a) Comparator
b) Comparable
c) Cloneable
d) Serializable
Correct Answer: b) Comparable
Explanation:
The Comparable interface compareTo() method defines the natural ordering of objects.

23. Which interface should be implemented to define a custom ordering for objects?

a) Comparable
b) Comparator
c) Collection
d) Iterator
Correct Answer: b) Comparator
Explanation:
The Comparator interface compare() method defines a custom external ordering.

24. Which class provides thread-safe access to a hash map with high concurrency?

a) HashMap
b) Hashtable
c) ConcurrentHashMap
d) SynchronizedMap
Correct Answer: c) ConcurrentHashMap
Explanation:
ConcurrentHashMap allows concurrent read and update operations without locking the entire map.

25. Which interface supports element insertion and removal at both ends?

a) Queue
b) List
c) Deque
d) Set
Correct Answer: c) Deque
Explanation:
Deque (Double Ended Queue) supports insertion, removal, and retrieval of elements at both ends.

26. What does the peek() method of a Queue do?

a) Removes and returns the head
b) Returns the head without removing it
c) Adds an element to the head
d) Checks if the queue is empty
Correct Answer: b) Returns the head without removing it
Explanation:
peek() retrieves, but does not remove, the head of the queue. It returns null if empty.

27. What is the difference between poll() and remove() in a Queue?

a) poll() throws an exception if empty; remove() returns null
b) poll() returns null if empty; remove() throws an exception
c) There is no difference
d) poll() removes from tail; remove() removes from head
Correct Answer: b) poll() returns null if empty; remove() throws an exception
Explanation:
Both remove and return the head, but remove() throws NoSuchElementException when empty.

28. What is the load factor in a HashMap?

a) The initial capacity of the map
b) The threshold at which the map is resized
c) The number of elements currently in the map
d) The maximum number of collisions allowed
Correct Answer: b) The threshold at which the map is resized
Explanation:
The load factor determines when the hash table is resized. The default is 0.75.

29. What is the default initial capacity of a HashMap?

a) 10
b) 16
c) 32
d) 64
Correct Answer: b) 16
Explanation:
The default initial capacity of a HashMap is 16.

30. Which of the following does NOT implement the Collection interface?

a) LinkedList
b) Vector
c) Properties
d) PriorityQueue
Correct Answer: c) Properties
Explanation:
Properties extends Hashtable, which implements Map, not Collection.

31. Which collection class allows you to associate its elements with an index?

a) Set
b) Map
c) List
d) Queue
Correct Answer: c) List
Explanation:
List is an ordered collection where elements can be accessed using a zero-based index.

32. What happens when you add an element to an ArrayList that has reached its capacity?

a) Throws an OutOfMemoryError
b) Throws an IndexOutOfBoundsException
c) Creates a larger array and copies the elements
d) Overwrites the first element
Correct Answer: c) Creates a larger array and copies the elements
Explanation:
ArrayList resizes itself dynamically by typically increasing its capacity by 50% when full.

33. Which list is best suited for frequent insertions and deletions in the middle?

a) ArrayList
b) Vector
c) LinkedList
d) CopyOnWriteArrayList
Correct Answer: c) LinkedList
Explanation:
LinkedList provides O(1) time complexity for insertions and deletions once the location is found.

34. Which legacy class implements the Map interface?

a) Vector
b) Stack
c) Hashtable
d) BitSet
Correct Answer: c) Hashtable
Explanation:
Hashtable is a synchronized legacy class that implements the Map interface.

35. Can a Hashtable have a null key or null value?

a) Yes, both can be null
b) Only the key can be null
c) Only the value can be null
d) No, neither can be null
Correct Answer: d) No, neither can be null
Explanation:
Unlike HashMap, Hashtable does not permit null keys or null values.

36. Which of these is not a method of the Collection interface?

a) add()
b) clear()
c) get()
d) size()
Correct Answer: c) get()
Explanation:
The get() method belongs to the List interface, not the generic Collection interface.

37. What is the time complexity of the add() method in a HashSet?

a) O(1)
b) O(n)
c) O(log n)
d) O(n^2)
Correct Answer: a) O(1)
Explanation:
HashSet operations like add, remove, and contains generally take O(1) time.

38. Which method is used to sort a List of Objects?

a) Arrays.sort()
b) Collections.sort()
c) List.sortList()
d) List.order()
Correct Answer: b) Collections.sort()
Explanation:
The java.util.Collections.sort() method is used to sort Lists.

39. How do you create a synchronized wrapper for an ArrayList?

a) new SynchronizedList(new ArrayList<>())
b) Collections.synchronizedList(new ArrayList<>())
c) new ArrayList<>(true)
d) Arrays.synchronizedList(new ArrayList<>())
Correct Answer: b) Collections.synchronizedList(new ArrayList<>())
Explanation:
The Collections utility class provides methods to create thread-safe wrappers for collections.

40. Which map is based on identity (==) rather than equality for its keys?

a) HashMap
b) TreeMap
c) LinkedHashMap
d) IdentityHashMap
Correct Answer: d) IdentityHashMap
Explanation:
IdentityHashMap uses reference equality (==) instead of object equality to compare keys.

41. Which queue implementation orders elements based on their natural ordering?

a) LinkedList
b) PriorityQueue
c) ArrayDeque
d) ConcurrentLinkedQueue
Correct Answer: b) PriorityQueue
Explanation:
PriorityQueue is an unbounded priority queue based on a priority heap.

42. Which of these classes implements the Deque interface?

a) PriorityQueue
b) Vector
c) ArrayDeque
d) HashSet
Correct Answer: c) ArrayDeque
Explanation:
ArrayDeque is a resizable-array implementation of the Deque interface.

43. Which is faster for basic operations as a stack/deque?

a) Stack
b) ArrayDeque
c) They are exactly the same
d) Depends on the number of elements
Correct Answer: b) ArrayDeque
Explanation:
ArrayDeque is faster than Stack because it is not synchronized.

44. Which interface does PriorityQueue implement?

a) Queue
b) Deque
c) List
d) Set
Correct Answer: a) Queue
Explanation:
PriorityQueue implements the Queue interface.

45. Which exception is thrown if you modify a Collections.unmodifiableList()?

a) IllegalArgumentException
b) UnsupportedOperationException
c) IllegalStateException
d) ConcurrentModificationException
Correct Answer: b) UnsupportedOperationException
Explanation:
Attempting to modify an unmodifiable collection results in an UnsupportedOperationException.

46. What does the retainAll() method do in a Collection?

a) Removes all elements in the collection
b) Retains only the elements that are in the specified collection
c) Merges two collections
d) Checks if all elements exist in another collection
Correct Answer: b) Retains only the elements that are in the specified collection
Explanation:
retainAll() removes elements not contained in the specified collection (Intersection).

47. Which Iterator method is used to safely remove an element during iteration?

a) delete()
b) remove()
c) poll()
d) pop()
Correct Answer: b) remove()
Explanation:
The Iterator.remove() method safely removes the last element returned by the iterator.

48. Which method can be used to reverse the order of elements in a List?

a) List.reverse()
b) Collections.reverse()
c) Arrays.reverse()
d) Collections.sortReverse()
Correct Answer: b) Collections.reverse()
Explanation:
Collections.reverse() reverses the order of the elements in the specified list.

49. Which concurrent collection uses a copy-on-write strategy?

a) ConcurrentHashMap
b) CopyOnWriteArrayList
c) Vector
d) ConcurrentLinkedQueue
Correct Answer: b) CopyOnWriteArrayList
Explanation:
CopyOnWriteArrayList creates a fresh copy of the underlying array upon every mutative operation.

50. When is CopyOnWriteArrayList a good choice?

a) When there are frequent writes and few reads
b) When there are frequent reads and few writes
c) When memory is severely limited
d) When you need to maintain a sorted list
Correct Answer: b) When there are frequent reads and few writes
Explanation:
Because writes copy the entire array, it is only efficient when reads vastly outnumber writes.

51. Which Map can automatically remove the least recently accessed entries?

a) HashMap
b) LinkedHashMap
c) WeakHashMap
d) TreeMap
Correct Answer: b) LinkedHashMap
Explanation:
By overriding removeEldestEntry(), LinkedHashMap can act as an LRU cache.

52. Which Map implementation holds weak references to its keys?

a) HashMap
b) IdentityHashMap
c) WeakHashMap
d) ConcurrentHashMap
Correct Answer: c) WeakHashMap
Explanation:
WeakHashMap keys are weakly referenced and can be garbage collected if no other strong references exist.

53. Which method finds the maximum element in a Collection using natural ordering?

a) Collection.max()
b) Collections.max()
c) Math.max()
d) Arrays.max()
Correct Answer: b) Collections.max()
Explanation:
Collections.max() returns the maximum element of the given collection.

54. What is the return type of Map.entrySet()?

a) List<Map.Entry<K,V>>
b) Set
c) Collection
d) Set<Map.Entry<K,V>>
Correct Answer: d) Set<Map.Entry<K,V>>
Explanation:
entrySet() returns a Set view of the map's key-value entries.

55. What does the Map.keySet() method return?

a) A List of keys
b) A Set of keys
c) An Array of keys
d) A Collection of keys
Correct Answer: b) A Set of keys
Explanation:
keySet() returns a Set view of the keys contained in the map, because keys must be unique.

56. What does the Map.values() method return?

a) A Set of values
b) A List of values
c) A Collection of values
d) An Iterator of values
Correct Answer: c) A Collection of values
Explanation:
values() returns a Collection view of the values in the map, as values can be duplicated.

57. In Java 8+, how can you compute a value if a key is missing in a Map?

a) putIfMissing()
b) computeIfAbsent()
c) getOrDefault()
d) addIfAbsent()
Correct Answer: b) computeIfAbsent()
Explanation:
computeIfAbsent() computes a new value for the specified key if it is not already associated with a value.

58. Which interface represents an element that is returned by Iterator?

a) No specific interface, it returns a generic type
b) Element
c) Node
d) Entry
Correct Answer: a) No specific interface, it returns a generic type
Explanation:
Iterator's next() method returns an element of the generic type E.

59. Which interface provides methods for navigating a sorted set?

a) SortedSet
b) NavigableSet
c) TreeSet
d) OrderedSet
Correct Answer: b) NavigableSet
Explanation:
NavigableSet extends SortedSet to report closest matches for given search targets.

60. What happens in a HashMap if multiple keys have the same hash code?

a) An Exception is thrown
b) The keys are overwritten
c) A collision occurs and elements are chained
d) The hash code is recalculated
Correct Answer: c) A collision occurs and elements are chained
Explanation:
HashMap handles collisions by chaining elements in a linked list or a balanced tree.

61. At what threshold does a HashMap bucket switch to a tree in Java 8?

a) 4
b) 8
c) 16
d) 32
Correct Answer: b) 8
Explanation:
The TREEIFY_THRESHOLD is 8, meaning a bin turns into a tree structure at 8 elements.

62. What is the backing data structure of a HashSet?

a) Array
b) LinkedList
c) HashMap
d) TreeSet
Correct Answer: c) HashMap
Explanation:
HashSet uses an instance of HashMap internally, where elements act as keys.

63. Which enum collection is optimized specifically for Enum keys?

a) EnumList
b) EnumMap
c) EnumSet
d) EnumHash
Correct Answer: b) EnumMap
Explanation:
EnumMap is a specialized Map implementation for use with enum type keys.

64. Which of these collections is fast for representing a set of Enum values?

a) EnumSet
b) HashSet
c) TreeSet
d) BitSet
Correct Answer: a) EnumSet
Explanation:
EnumSet is a specialized Set implementation for enum types, represented as bit vectors.

65. Which class represents a fixed-size list from an array?

a) Arrays.toList()
b) Arrays.asList()
c) Collections.fixedList()
d) List.of()
Correct Answer: b) Arrays.asList()
Explanation:
Arrays.asList() returns a fixed-size list backed by the specified array.

66. What exception is thrown when adding an element to Arrays.asList()?

a) IllegalArgumentException
b) UnsupportedOperationException
c) ArrayStoreException
d) NullPointerException
Correct Answer: b) UnsupportedOperationException
Explanation:
The list returned by Arrays.asList() is fixed-size. Structural modifications throw UnsupportedOperationException.

67. In Java 9, how can you create an immutable List?

a) List.create()
b) Collections.unmodifiableList()
c) List.of()
d) Arrays.immutableList()
Correct Answer: c) List.of()
Explanation:
Java 9 introduced List.of() to create unmodifiable lists easily.

68. How many null elements does Set.of() allow in Java 9+?

a) 0
b) 1
c) Any number
d) Depends on the generics
Correct Answer: a) 0
Explanation:
Factory methods like Set.of() do not allow null elements and throw NullPointerException.

69. Which Iterator method helps perform an action for each remaining element?

a) forEach()
b) forEachRemaining()
c) iterateAll()
d) consumeRemaining()
Correct Answer: b) forEachRemaining()
Explanation:
Iterator.forEachRemaining() performs the given action for each remaining element.

70. What interface complements Iterator for parallel traversal in Java 8?

a) Enumeration
b) Iterator
c) ListIterator
d) Spliterator
Correct Answer: d) Spliterator
Explanation:
Spliterator is an interface introduced in Java 8 to support parallel traversal.

71. Which method in the Collection interface returns a Spliterator?

a) split()
b) getSpliterator()
c) spliterator()
d) parallelIterator()
Correct Answer: c) spliterator()
Explanation:
The spliterator() method returns a Spliterator over the elements in the collection.

72. What is the primary purpose of a BlockingQueue?

a) To block duplicate elements
b) To block null elements
c) To wait for space or elements to become available
d) To provide synchronized sorting
Correct Answer: c) To wait for space or elements to become available
Explanation:
BlockingQueue supports operations that wait for the queue to become non-empty or for space.

73. Which class is a blocking queue backed by an array?

a) LinkedBlockingQueue
b) ArrayBlockingQueue
c) PriorityBlockingQueue
d) ConcurrentLinkedQueue
Correct Answer: b) ArrayBlockingQueue
Explanation:
ArrayBlockingQueue is a bounded blocking queue backed by an array.

74. Which collection throws ConcurrentModificationException on concurrent modification?

a) ConcurrentHashMap
b) CopyOnWriteArrayList
c) ArrayList
d) Vector
Correct Answer: c) ArrayList
Explanation:
ArrayList is not thread-safe and its fail-fast iterators throw ConcurrentModificationException.

75. What happens if you use put() on a HashMap with a key that already exists?

a) It ignores the operation
b) It throws an exception
c) It replaces and returns the old value
d) It replaces the old value and returns true
Correct Answer: c) It replaces and returns the old value
Explanation:
The put() method replaces the existing value for the key and returns the previous value.

76. Which method is used to remove all elements from a collection?

a) deleteAll()
b) clear()
c) empty()
d) removeAll()
Correct Answer: b) clear()
Explanation:
The clear() method removes all elements from the collection.

77. What is the difference between removeAll() and clear()?

a) There is no difference
b) removeAll() takes a collection of elements; clear() removes everything
c) clear() returns a boolean; removeAll() returns void
d) removeAll() only works on Sets
Correct Answer: b) removeAll() takes a collection of elements; clear() removes everything
Explanation:
removeAll(Collection) removes only the elements present in the specified collection, clear() empties it.

78. How can you shuffle a list randomly?

a) Collections.randomize(list)
b) List.shuffle()
c) Collections.shuffle(list)
d) Arrays.shuffle(list)
Correct Answer: c) Collections.shuffle(list)
Explanation:
Collections.shuffle() randomly permutes the specified list.

79. Which class provides a concurrent implementation of a navigable map?

a) TreeMap
b) ConcurrentHashMap
c) ConcurrentSkipListMap
d) SynchronizedTreeMap
Correct Answer: c) ConcurrentSkipListMap
Explanation:
ConcurrentSkipListMap provides a thread-safe, scalable concurrent implementation of NavigableMap.

80. Which interface must PriorityQueue elements implement if no Comparator is provided?

a) Comparator
b) Comparable
c) Cloneable
d) Serializable
Correct Answer: b) Comparable
Explanation:
If no Comparator is provided, the elements must implement Comparable.

81. What is the time complexity of the contains() method in an ArrayList?

a) O(1)
b) O(log n)
c) O(n)
d) O(n log n)
Correct Answer: c) O(n)
Explanation:
ArrayList must perform a linear search, which takes O(n) time.

82. Which structure does java.util.Properties extend?

a) HashMap
b) Hashtable
c) TreeMap
d) LinkedHashMap
Correct Answer: b) Hashtable
Explanation:
Properties extends Hashtable to maintain a list of string key-value pairs.

83. Which Collection is thread-safe but generally slower than ArrayList?

a) Vector
b) HashSet
c) LinkedList
d) TreeSet
Correct Answer: a) Vector
Explanation:
Vector is synchronized by default, making it thread-safe but slower than ArrayList.

84. What happens if a HashSet is modified while iterating over it via enhanced for-loop?

a) The loop finishes normally
b) It throws NullPointerException
c) It throws ConcurrentModificationException
d) The set becomes unsynchronized
Correct Answer: c) It throws ConcurrentModificationException
Explanation:
An enhanced for-loop uses an Iterator internally, which is fail-fast.

85. Which of the following is true regarding java.util.BitSet?

a) It implements the Set interface
b) It is not part of the Collection framework interface hierarchy
c) It implements Map
d) It only accepts boolean objects
Correct Answer: b) It is not part of the Collection framework interface hierarchy
Explanation:
BitSet is a utility class for bit arrays but does not implement Collection or Set.

86. Which class provides a memory-efficient Set for primitive consecutive integers?

a) BitSet
b) IntSet
c) HashSet
d) There is no built-in primitive collection in java.util
Correct Answer: d) There is no built-in primitive collection in java.util
Explanation:
The standard java.util package does not provide primitive specialization collections.

87. What does the toArray() method of Collection do?

a) Returns a primitive array
b) Returns an Object array containing all elements
c) Returns a String array
d) Converts the collection to an ArrayList
Correct Answer: b) Returns an Object array containing all elements
Explanation:
Collection.toArray() converts the collection into a standard array of Objects.

88. What does toArray(T[] a) do?

a) Fills the provided array or creates a new one of the same type
b) Throws an exception if the array is empty
c) Casts the collection to a generic array directly
d) Sorts the array before returning
Correct Answer: a) Fills the provided array or creates a new one of the same type
Explanation:
It returns an array containing all of the elements with the runtime type of the specified array.

89. Is null allowed in ArrayDeque?

a) Yes
b) No
c) Only at the beginning
d) Only at the end
Correct Answer: b) No
Explanation:
ArrayDeque does not permit null elements.

90. Can PriorityQueue hold null elements?

a) Yes
b) No
c) Only if a custom comparator is used
d) Yes, but they are sorted last
Correct Answer: b) No
Explanation:
PriorityQueue does not permit null elements because they cannot be compared.

91. What method gets a collection with zero elements safely and optimally?

a) new ArrayList<>()
b) Collections.emptyList()
c) List.null()
d) new List(0)
Correct Answer: b) Collections.emptyList()
Explanation:
Collections.emptyList() returns a static, immutable empty list.

92. How do you create a List with a single element natively in a memory efficient way?

a) Collections.singletonList(element)
b) new ArrayList<>(element)
c) List.one(element)
d) Arrays.asList(element)
Correct Answer: a) Collections.singletonList(element)
Explanation:
Collections.singletonList() creates an immutable list containing only the specified object.

93. In HashMap, what is the role of the hash function?

a) To sort the elements
b) To calculate the index of the bucket
c) To ensure thread safety
d) To guarantee element uniqueness
Correct Answer: b) To calculate the index of the bucket
Explanation:
The hash function processes the key to determine the bucket index.

94. Which interface specifies that a Map must guarantee element uniqueness?

a) Map
b) UniqueMap
c) Set
d) Maps guarantee key uniqueness, not elements (values)
Correct Answer: d) Maps guarantee key uniqueness, not elements (values)
Explanation:
Map ensures keys are unique, but it does not restrict duplicate values.

95. What happens if you insert an element into an ArrayList at an index greater than its size?

a) The list resizes and fills the gaps with null
b) It throws IndexOutOfBoundsException
c) It ignores the operation
d) It inserts the element at the end of the list
Correct Answer: b) It throws IndexOutOfBoundsException
Explanation:
You cannot insert at an index strictly greater than the current size of the ArrayList.

96. What is the best way to convert an array of objects to a java.util.List?

a) List.fromArray(arr)
b) Arrays.asList(arr)
c) Collections.toList(arr)
d) new List(arr)
Correct Answer: b) Arrays.asList(arr)
Explanation:
Arrays.asList() provides a bridge between array-based and collection-based APIs.

97. What is the primary difference between Comparable and Comparator?

a) Comparable defines natural ordering; Comparator defines custom ordering
b) Comparable is in java.util; Comparator is in java.lang
c) Comparable takes two objects; Comparator takes one
d) There is no functional difference
Correct Answer: a) Comparable defines natural ordering; Comparator defines custom ordering
Explanation:
Comparable is for default sorting, Comparator lets you define multiple external sorting strategies.

98. Which of these provides the fastest iteration?

a) ArrayList
b) LinkedList
c) HashSet
d) TreeSet
Correct Answer: a) ArrayList
Explanation:
ArrayList has contiguous memory allocation, making CPU cache access much faster for iteration.

99. What does Collections.disjoint(c1, c2) do?

a) Removes duplicates from c1 and c2
b) Returns true if the two collections have no elements in common
c) Merges c1 and c2
d) Disconnects an iterator
Correct Answer: b) Returns true if the two collections have no elements in common
Explanation:
Collections.disjoint returns true if the two specified collections have no elements in common.

100. Which of the following is true about java.util.Stack?

a) It extends Vector
b) It implements Queue
c) It is not thread-safe
d) It is an interface
Correct Answer: a) It extends Vector
Explanation:
Stack is a class that extends Vector to provide LIFO operations.

101. What is the purpose of the trimToSize() method in ArrayList?

a) To reduce the capacity of the instance to its current size
b) To remove null elements
c) To limit the maximum size
d) To truncate strings in the list
Correct Answer: a) To reduce the capacity of the instance to its current size
Explanation:
trimToSize() trims the capacity of the ArrayList to its current size to minimize storage.

102. Which functional interface does the removeIf() method take as an argument?

a) Function
b) Consumer
c) Predicate
d) Supplier
Correct Answer: c) Predicate
Explanation:
removeIf(Predicate filter) removes all elements that satisfy the given predicate.

103. How do you replace all values in a List that match a condition using Java 8?

a) List.replaceIf()
b) List.replaceAll()
c) Collections.replace()
d) List.map()
Correct Answer: b) List.replaceAll()
Explanation:
replaceAll() replaces each element with the result of applying an operator to that element.

104. Which map provides get() operations that do not lock?

a) ConcurrentHashMap
b) Hashtable
c) Collections.synchronizedMap()
d) TreeMap
Correct Answer: a) ConcurrentHashMap
Explanation:
ConcurrentHashMap allows lock-free read operations while being thread-safe.

105. What does the Map.merge() method do in Java 8?

a) Merges two maps completely
b) If key absent, puts value; if present, uses a function to merge values
c) Combines a list into a map
d) Replaces all duplicates
Correct Answer: b) If key absent, puts value; if present, uses a function to merge values
Explanation:
Map.merge() updates the value using the provided function to merge the existing and new values.

106. Can you use primitive types like int or double in a Collection?

a) Yes
b) No, you must use their wrapper classes
c) Only in specialized collections
d) Yes, but they are converted to Strings
Correct Answer: b) No, you must use their wrapper classes
Explanation:
Java Collections can only hold object references. Primitives are autoboxed into their wrapper classes.

107. Which interface provides the offer(), poll(), and peek() methods?

a) List
b) Set
c) Queue
d) Map
Correct Answer: c) Queue
Explanation:
The Queue interface defines offer, poll, and peek for working with elements safely.

108. What is the output of Collections.binarySearch() if the element is not found?

a) -1
b) 0
c) A negative integer representing the insertion point
d) Throws NoSuchElementException
Correct Answer: c) A negative integer representing the insertion point
Explanation:
It returns a negative value that indicates the exact insertion point where the element should go.

109. When using binarySearch(), what is required of the List?

a) It must be an ArrayList
b) It must be synchronized
c) It must be sorted in ascending order prior to the call
d) It must not contain null elements
Correct Answer: c) It must be sorted in ascending order prior to the call
Explanation:
binarySearch() only works reliably if the list has already been sorted.

110. Which of these collections allow multiple threads to iterate without throwing exceptions?

a) ArrayList
b) HashSet
c) CopyOnWriteArrayList
d) HashMap
Correct Answer: c) CopyOnWriteArrayList
Explanation:
CopyOnWriteArrayList iterators rely on a snapshot of the array and will not throw ConcurrentModificationException.

111. What does the Map.getOrDefault() method do?

a) Adds the default value if the key is missing
b) Returns the mapped value, or defaultValue if the map contains no mapping
c) Throws an exception if key not found
d) Deletes the key
Correct Answer: b) Returns the mapped value, or defaultValue if the map contains no mapping
Explanation:
It provides a safe way to fetch a value with a fallback, without modifying the map.
← Previous: Java Basics MCQs for Developer Interviews & Certification
Next →: Java Control Flow / If Else MCQs
Latest Java OOP MCQs

Latest Java OOP MCQs

Object-Oriented Programming (OOP) is the core paradigm underlying Java, designed to organize software design around data, or objects, rather than…

By MCQs Generator
No Image

400+ C++ Interview Questions MCQ

1. Which of these interfaces is the root of the collection hierarchy? a) Collection b) List c) Root d) Iterable…

By MCQs Generator
JavaScript DOM Manipulation MCQs

JavaScript DOM Manipulation MCQs

The Document Object Model (DOM) is a cross-platform programming interface that treats HTML and XML documents as a hierarchical tree…

By MCQs Generator