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+).
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.
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.
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.
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.
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.
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.
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.
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());
EXPLANATION
PriorityQueue orders elements based on their natural ordering (min-heap by default). peek() returns 3, the minimum element.
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.