Java Programming — Collections Framework
Java OOP, collections, multithreading
22 Questions 10 Topics Take Test
Advertisement
Showing 1–10 of 22 questions in Collections Framework
In Java 2024, if you need a collection that automatically removes entries based on garbage collection patterns, which would you choose?
A WeakHashMap
B IdentityHashMap
C EnumMap
D ConcurrentHashMap
Correct Answer:  A. WeakHashMap
EXPLANATION

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

Take Test
Consider this code: List list = new CopyOnWriteArrayList(); list.add("A"); list.add("B"); Iterator itr = list.iterator(); list.add("C"); System.out.println(itr.next());
A Throws ConcurrentModificationException
B Prints 'A'
C Prints 'C'
D Throws NoSuchElementException
Correct Answer:  B. Prints 'A'
EXPLANATION

CopyOnWriteArrayList creates a snapshot for iterators, so modifications after iterator creation don't affect the iteration. Iterator sees only original elements: A and B.

Take Test
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.

Take Test
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.

Take Test
In Java 2024-25, which enhancement improved record support in collections?
A Records automatically implement Comparable
B Records can be used as sealed collection types with pattern matching
C Records eliminate the need for custom Comparators
D Records are automatically thread-safe
Correct Answer:  B. Records can be used as sealed collection types with pattern matching
EXPLANATION

Java 21+ enhanced sealed classes and pattern matching, allowing records to be used with pattern matching in collection operations and switch statements for type-safe data handling.

Take Test
Advertisement
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 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
What does the spliterator() method introduced in Java 8 provide over Iterator?
A Better support for parallel processing
B Faster sequential iteration
C Backward iteration capability
D Automatic synchronization
Correct Answer:  A. Better support for parallel processing
EXPLANATION

Spliterator provides split() capability enabling efficient parallel stream processing with characteristics advertisement.

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