Java Programming — Collections Framework
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 71–80 of 100 questions in Collections Framework
What does the removeIf() method in Collection interface do?
A Removes the first element matching predicate
B Removes all elements matching predicate
C Removes elements at specific index if predicate is true
D Removes duplicates based on predicate
Correct Answer:  B. Removes all elements matching predicate
EXPLANATION

removeIf(Predicate) removes all elements that satisfy the given predicate condition (Java 8+).

Take Test
Consider a scenario where you need fast random access and frequent insertions in the middle. Which collection is most suitable?
A ArrayList
B LinkedList
C Vector
D CopyOnWriteArrayList
Correct Answer:  B. LinkedList
EXPLANATION

LinkedList provides O(1) insertions/deletions at any position. ArrayList is O(n) for middle insertions due to shifting.

Take Test
Which of the following is a fail-fast iterator behavior?
A Iterator stops when structural modification occurs
B Iterator throws exception when structural modification occurs
C Iterator skips modified elements silently
D Iterator creates a snapshot of collection
Correct Answer:  B. Iterator throws exception when structural modification occurs
EXPLANATION

Fail-fast iterators throw ConcurrentModificationException when collection is modified during iteration.

Take Test
What is the time complexity of contains() operation in HashSet?
A O(1) average case, O(n) worst case
B O(n) always
C O(log n)
D O(n log n)
Correct Answer:  A. O(1) average case, O(n) worst case
EXPLANATION

HashSet uses hash table internally. Average case is O(1), worst case O(n) when hash collisions occur.

Take Test
In the context of streams and collections, which method returns a Sequential Stream in Java 8+?
A collection.parallelStream()
B collection.stream()
C Stream.of(collection)
D collection.iterator().stream()
Correct Answer:  B. collection.stream()
EXPLANATION

collection.stream() returns a sequential stream, while parallelStream() returns a parallel stream.

Take Test
What is the output of the following code?
List list = Arrays.asList("A", "B", "C");
list.add("D");
System.out.println(list.size());
A 4
B 3
C Throws UnsupportedOperationException
D Throws ArrayIndexOutOfBoundsException
Correct Answer:  C. Throws UnsupportedOperationException
EXPLANATION

Arrays.asList() returns a fixed-size list backed by array. add() operation is not supported.

Take Test
Which collection class implements NavigableMap interface?
A HashMap
B TreeMap
C LinkedHashMap
D EnumMap
Correct Answer:  B. TreeMap
EXPLANATION

TreeMap implements NavigableMap which provides methods like higherKey(), lowerKey(), floorEntry(), etc.

Take Test
Consider the following code. What is the behavior?
Collection col = new ArrayList();
col.add("Java");
Iterator it = col.iterator();
while(it.hasNext()) {
String s = it.next();
col.remove(s);
}
A Executes successfully
B Throws ConcurrentModificationException
C Removes all elements
D Throws NullPointerException
Correct Answer:  B. Throws ConcurrentModificationException
EXPLANATION

Modifying collection directly while iterating throws ConcurrentModificationException. Use iterator.remove() instead.

Take Test
What will be the result of executing this code?
PriorityQueue pq = new PriorityQueue();
pq.add(5);
pq.add(3);
pq.add(7);
System.out.println(pq.peek());
A 5
B 3
C 7
D Null
Correct Answer:  B. 3
EXPLANATION

PriorityQueue orders elements based on their natural ordering (min-heap by default). peek() returns 3, the minimum element.

Take Test
Which of the following statements about Queue interface is correct?
A Queue follows LIFO principle
B Queue.poll() throws exception if queue is empty
C Queue.offer() returns boolean instead of throwing exception
D Queue elements are accessed randomly
Correct Answer:  C. Queue.offer() returns boolean instead of throwing exception
EXPLANATION

offer() returns false if element cannot be added, while add() throws exception. poll() returns null if empty.

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