Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

476 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 361–370 of 476
Topics in Java Programming
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
What is the time complexity of add() operation in ArrayList?
A O(1) amortized
B O(n)
C O(log n)
D O(n log n)
Correct Answer:  A. O(1) amortized
EXPLANATION

ArrayList.add() has O(1) amortized time complexity. When capacity is exceeded, resizing takes O(n).

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