Iterator vs. ListIterator in Java: Which statements are correct about their capabilities and APIs?
-
A2, 3, 4 and 5
-
B1, 3, 4 and 5
-
C3, 4 and 5
-
D1, 2 and 3
-
E1 and 2 only
Answer
Correct Answer: 1, 3, 4 and 5
Explanation
Introduction / Context: Java provides Iterator for one-way traversal and ListIterator for bidirectional traversal and in-place modification of lists. This question checks which capabilities belong to which interface.
Given Data / Assumptions:
- Statements include: the methods of
Iterator, inheritance relationships, and features ofListIterator.
Concept / Approach: Iterator declares exactly three methods: hasNext(), next(), and remove(). ListIterator extends Iterator (not List) and adds backward traversal (hasPrevious(), previous()), modification methods (add(), set(), and remove() inherited), and positional queries (nextIndex(), previousIndex()).
Step-by-Step Solution:
1 is true: Iterator has exactly 3 methods.2 is false: ListIterator extends Iterator, not List.3 is true: ListIterator supports forward/backward traversal.4 is true: ListIterator can modify the list during iteration.5 is true: ListIterator can report its current index positions.Verification / Alternative check: The Javadoc for java.util.ListIterator lists these additional methods explicitly and shows it extends Iterator.
Why Other Options Are Wrong:
- Options containing 2 are incorrect because ListIterator does not extend List.
Common Pitfalls: Confusing List (a collection type) with ListIterator (an iterator over lists), and forgetting the backward traversal capability.
Final Answer: 1, 3, 4 and 5