Java Programming — Collections Framework
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 41–50 of 100 questions in Collections Framework
What exception is thrown when you try to add a null value to a TreeSet?
A NullPointerException
B IllegalArgumentException
C ClassCastException
D UnsupportedOperationException
Correct Answer:  A. NullPointerException
EXPLANATION

TreeSet uses comparator or natural ordering, which cannot handle null values, throwing NullPointerException during insertion.

Take Test
Which method of PriorityQueue returns the element without removing it?
A poll()
B remove()
C peek()
D pop()
Correct Answer:  C. peek()
EXPLANATION

peek() returns the head element without removing it. poll() removes and returns, remove() throws exception if empty, and pop() is not a PriorityQueue method.

Take Test
What is the difference between ArrayList and Vector?
A ArrayList is synchronized, Vector is not
B Vector is synchronized, ArrayList is not
C Both are synchronized
D Neither is synchronized
Correct Answer:  B. Vector is synchronized, ArrayList is not
EXPLANATION

Vector is a legacy synchronized collection, while ArrayList is unsynchronized but faster. For thread-safety with ArrayList, use Collections.synchronizedList().

Take Test
What is the time complexity of get() operation in TreeMap?
A O(1)
B O(log n)
C O(n)
D O(n log n)
Correct Answer:  B. O(log n)
EXPLANATION

TreeMap is based on Red-Black tree, so get(), put(), and remove() operations have O(log n) time complexity due to tree traversal.

Take Test
Which of the following maintains insertion order in Java?
A HashSet
B TreeSet
C LinkedHashSet
D ConcurrentHashMap
Correct Answer:  C. LinkedHashSet
EXPLANATION

LinkedHashSet maintains insertion order using a doubly-linked list. HashSet does not maintain order, TreeSet maintains sorted order, and ConcurrentHashMap doesn't guarantee insertion order.

Take Test
What is the underlying data structure of HashMap in Java?
A Balanced Binary Tree
B Hash Table with Buckets
C Linked List
D Array
Correct Answer:  B. Hash Table with Buckets
EXPLANATION

HashMap uses a hash table with buckets (arrays of linked lists or red-black trees) to store key-value pairs for efficient O(1) average lookup time.

Take Test
Which interface in Java Collections Framework does not allow duplicate elements?
A List
B Set
C Queue
D Map
Correct Answer:  B. Set
EXPLANATION

Set interface explicitly does not allow duplicate elements. List allows duplicates, Queue is for ordered collections, and Map stores key-value pairs.

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