Home Subjects Java Programming Collections Framework

Java Programming
Collections Framework

Java OOP, collections, multithreading

22 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 11–20 of 22
Topics in Java Programming
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.

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²).

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.

Test
What is the time complexity of subList() operation in ArrayList?
A O(1) - returns a view
B O(n) - creates a copy
C O(log n)
D O(n log n)
Correct Answer:  A. O(1) - returns a view
EXPLANATION

subList() returns a view (structural reference) to the original list in O(1) time, not a copy.

Test
What is the behavior when concurrent modification occurs during iteration in ConcurrentHashMap vs HashMap?
A Both throw ConcurrentModificationException
B ConcurrentHashMap allows safe iteration, HashMap throws exception
C Neither throws exception
D HashMap allows safe iteration, ConcurrentHashMap throws exception
Correct Answer:  B. ConcurrentHashMap allows safe iteration, HashMap throws exception
EXPLANATION

ConcurrentHashMap uses segment-based locking allowing safe iteration during concurrent modifications. HashMap throws ConcurrentModificationException.

Test
In Java 16+, what feature improved collection performance by allowing value-based classes?
A Records with Collections
B Sealed classes restriction
C Pattern matching in iteration
D Stream API enhancements
Correct Answer:  A. Records with Collections
EXPLANATION

Records (Java 14+) provide compact syntax for data carriers used with collections, improving performance and readability.

Test
In a multi-threaded environment, if you need a thread-safe list that allows concurrent reads, which is optimal?
A Collections.synchronizedList(new ArrayList())
B new CopyOnWriteArrayList()
C new Vector()
D new LinkedList()
Correct Answer:  B. new CopyOnWriteArrayList()
EXPLANATION

CopyOnWriteArrayList is optimal for read-heavy concurrent operations. It creates copy on write, allowing safe concurrent reads.

Test
Consider implementing a Comparator for custom sorting in reverse order. Which approach is correct?
List list = Arrays.asList(5, 2, 8, 1);
A Collections.sort(list, Collections.reverseOrder());
B Collections.sort(list, (a,b) -> b.compareTo(a));
C Collections.sort(list, (a,b) -> a.compareTo(b) * -1);
D All of above
Correct Answer:  D. All of above
EXPLANATION

All three approaches work correctly for reverse sorting. Options A, B, and C are all valid Java code.

Test
What is the behavior of get() method in LinkedHashMap with accessOrder=true?
A Returns element without affecting order
B Moves accessed element to end (updates access order)
C Throws exception
D Returns null for accessed elements
Correct Answer:  B. Moves accessed element to end (updates access order)
EXPLANATION

LinkedHashMap with accessOrder=true maintains access-order. get() moves the element to the end (LRU cache behavior).

Test
What is the worst-case time complexity of HashMap.get() when hash collisions occur?
A O(1)
B O(log n)
C O(n)
D O(n log n)
Correct Answer:  C. O(n)
EXPLANATION

In worst case with all collisions stored in linked list, get() becomes O(n). From Java 8, if collisions exceed threshold, linked list converts to balanced tree, making it O(log n).

Test
IGET
IGET AI
Online · Exam prep assistant
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