Java Programming
Java OOP, collections, multithreading
212 Questions 10 Topics Take Test
Advertisement
Showing 151–160 of 212 questions
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
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
In a scenario where you need to maintain a mapping with LRU (Least Recently Used) eviction policy, which class should be extended?
A LinkedHashMap with override of removeEldestEntry()
B TreeMap
C WeakHashMap
D ConcurrentHashMap
Correct Answer:  A. LinkedHashMap with override of removeEldestEntry()
EXPLANATION

LinkedHashMap can be extended and removeEldestEntry() overridden to implement LRU cache eviction policy.

Take Test
What is the worst-case time complexity of QuickSort when used with Collections.sort()?
A O(n log n)
B O(n²)
C O(n)
D O(log n)
Correct Answer:  B. O(n²)
EXPLANATION

Collections.sort() uses TimSort (hybrid of merge and insertion sort) with O(n log n) worst-case, but QuickSort's worst-case is O(n²).

Take Test
Which stream terminal operation preserves encounter order in parallel streams for Collections?
A collect() with unordered Collector
B collect() with CONCURRENT and UNORDERED characteristics
C forEach() directly
D forEachOrdered()
Correct Answer:  D. forEachOrdered()
EXPLANATION

forEachOrdered() preserves encounter order even in parallel streams, while forEach() doesn't guarantee order.

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