Home Subjects Java Programming Collections Framework

Java Programming
Collections Framework

Java OOP, collections, multithreading

100 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 51–60 of 100
Topics in Java Programming
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
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.

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

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

Take Test
What is the output of Collections.frequency(list, null) if list contains null elements?
A Returns count of null elements
B Throws NullPointerException
C Returns 0
D Throws UnsupportedOperationException
Correct Answer:  A. Returns count of null elements
EXPLANATION

Collections.frequency() correctly handles null values and returns the count of null elements in the collection.

Take Test
Which method of NavigableSet returns the greatest element strictly less than the given element?
A floor()
B lower()
C higher()
D ceiling()
Correct Answer:  B. lower()
EXPLANATION

lower() returns the greatest element < given element. floor() returns <= element.

Take Test
What exception is thrown by Iterator.next() when there are no more elements?
A NoSuchElementException
B IndexOutOfBoundsException
C NullPointerException
D ConcurrentModificationException
Correct Answer:  A. NoSuchElementException
EXPLANATION

Iterator.next() throws NoSuchElementException when hasNext() returns false and next() is called.

Take Test
Consider a scenario: You need a data structure that maintains elements in sorted order and allows fast insertion/deletion. Which is optimal?
A TreeSet
B ArrayList
C HashSet
D LinkedList
Correct Answer:  A. TreeSet
EXPLANATION

TreeSet maintains sorted order (O(log n) insertion/deletion) using a Red-Black tree structure.

Take Test
What is the difference between Iterator and ListIterator?
A ListIterator can traverse in both directions
B Iterator is faster than ListIterator
C ListIterator works only with ArrayList
D Both are identical
Correct Answer:  A. ListIterator can traverse in both directions
EXPLANATION

ListIterator extends Iterator and provides bidirectional traversal using previous() and next() methods.

Take Test
Which collection maintains insertion order and also provides thread-safety?
A ConcurrentHashMap
B Collections.synchronizedList()
C LinkedHashMap with synchronization
D CopyOnWriteArrayList
Correct Answer:  D. CopyOnWriteArrayList
EXPLANATION

CopyOnWriteArrayList maintains insertion order and is thread-safe. It creates a copy of the array on modification.

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