Java Programming — Collections Framework
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 31–40 of 100 questions in Collections Framework
What is the primary purpose of the Comparator interface in Collections Framework?
A To compare two objects and return equality
B To define custom ordering for sorting collections
C To prevent duplicate elements
D To enhance performance of HashMap
Correct Answer:  B. To define custom ordering for sorting collections
EXPLANATION

Comparator interface allows defining custom comparison logic for sorting. It provides compare(T o1, T o2) method to establish custom ordering independent of the object's natural order.

Take Test
Which of these operations is guaranteed to be atomic in ConcurrentHashMap?
A if(!map.containsKey(key)) map.put(key, value);
B map.putIfAbsent(key, value);
C map.get(key) followed by map.put(key, newValue);
D map.remove(key) followed by map.put(key, value);
Correct Answer:  B. map.putIfAbsent(key, value);
EXPLANATION

putIfAbsent() is an atomic operation. Options A and C-D are compound operations that are not atomic and can have race conditions between checks and modifications.

Take Test
What is the behavior of WeakHashMap when a key is no longer strongly referenced?
A The entry remains in the map forever
B The entry is eligible for garbage collection
C A NullPointerException is thrown
D The key is automatically reset to null
Correct Answer:  B. The entry is eligible for garbage collection
EXPLANATION

WeakHashMap uses weak references for keys. When a key is no longer strongly referenced elsewhere, it becomes eligible for garbage collection, and its entry is removed from the map.

Take Test
How does Java 8+ handle hash collisions in HashMap differently?
A Uses double hashing
B Converts bucket linked list to Red-Black tree when size exceeds threshold
C Increases hash table size automatically
D Uses linear probing
Correct Answer:  B. Converts bucket linked list to Red-Black tree when size exceeds threshold
EXPLANATION

Java 8+ introduced tree nodes: when a bucket's linked list size exceeds TREEIFY_THRESHOLD (8), it converts to a Red-Black tree for better performance (O(log n) vs O(n)).

Take Test
What is the advantage of using LinkedList over ArrayList for frequent insertions at the beginning?
A LinkedList has O(1) insertion at beginning, ArrayList has O(n)
B ArrayList is always faster
C LinkedList uses less memory
D LinkedList supports null values while ArrayList doesn't
Correct Answer:  A. LinkedList has O(1) insertion at beginning, ArrayList has O(n)
EXPLANATION

LinkedList provides O(1) insertion at the beginning (head), while ArrayList requires O(n) time to shift elements. Both support nulls and LinkedList uses more memory due to node pointers.

Take Test
Which method in NavigableMap returns a view of the map in descending order?
A descendingMap()
B reverseMap()
C inverseMap()
D sortedMap()
Correct Answer:  A. descendingMap()
EXPLANATION

descendingMap() method of NavigableMap returns a reverse-ordered view. reverseMap() doesn't exist in Collections Framework.

Take Test
Consider: Set set = new HashSet(Arrays.asList(1,2,3,2,1)); System.out.println(set.size()); What is the output?
A 5
B 3
C 2
D 1
Correct Answer:  B. 3
EXPLANATION

HashSet removes duplicates. The list [1,2,3,2,1] is converted to a set containing only unique elements {1,2,3}, so size is 3.

Take Test
What does the containsAll() method of Collection return for an empty collection?
A true
B false
C null
D throws exception
Correct Answer:  A. true
EXPLANATION

containsAll() returns true for empty collections because mathematically, all elements of an empty set are contained in any set (vacuous truth).

Take Test
In what scenario would you use a ConcurrentHashMap instead of HashMap?
A When you need faster performance
B When you need thread-safe operations without locking entire map
C When you need to store null values
D When you need to maintain insertion order
Correct Answer:  B. When you need thread-safe operations without locking entire map
EXPLANATION

ConcurrentHashMap uses bucket-level locking (segment locking in Java 7, node-level in Java 8+) allowing concurrent reads/writes, making it thread-safe without synchronizing the entire map.

Take Test
Which Collections utility method creates an immutable empty list?
A Collections.emptyList()
B Collections.unmodifiableList(new ArrayList())
C new ArrayList()
D Arrays.asList()
Correct Answer:  A. Collections.emptyList()
EXPLANATION

Collections.emptyList() returns an immutable, empty list. Option B also works but is more verbose. Option C creates mutable list, and Option D creates mutable list from array.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips