Which interfaces does Vector.elements() return, and how do instanceof tests evaluate? import java.util.*; class H { public static void main (String[] args) { Object x = new Vector().elements(); System.out.print((x instanceof Enumeration) + ","); System.out.print((x instanceof Iterator) + ","); System.out.print(x instanceof ListIterator); } }
-
APrints: false,false,false
-
BPrints: false,false,true
-
CPrints: false,true,false
-
DPrints: true,false,false
-
EPrints: true,true,false
Answer
Correct Answer: Prints: true,false,false
Explanation
Introduction / Context:
This tests knowledge of legacy collections vs. modern iterators. Vector.elements() returns an Enumeration, not an Iterator or ListIterator.
Given Data / Assumptions:
x = new Vector().elements()produces a legacyEnumeration.- The program checks three
instanceofrelations and prints their boolean results separated by commas.
Concept / Approach: Enumeration pre-dates the Java Collections Framework. It is distinct from Iterator and ListIterator. Therefore, only the first check is true.
Step-by-Step Solution: x instanceof Enumeration → true. x instanceof Iterator → false. x instanceof ListIterator → false. Printed line: true,false,false.
Verification / Alternative check: Replace elements() with iterator() from a modern collection (e.g., new ArrayList().iterator()) and observe the changed results.
Why Other Options Are Wrong: Any output claiming Iterator or ListIterator is true misunderstands the return type of elements().
Common Pitfalls: Equating Enumeration with Iterator; forgetting that ListIterator is a specialized bidirectional iterator available via listIterator(), not iterator() or elements().
Final Answer: Prints: true,false,false