Java Programming - Objects and Collections - Discussion


What will be the output of the program?

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); 

[A]. Prints: false,false,false
[B]. Prints: false,false,true
[C]. Prints: false,true,false
[D]. Prints: true,false,false

Answer: Option D


The Vector.elements method returns an Enumeration over the elements of the vector. Vector implements the List interface and extends AbstractList so it is also possible to get an Iterator over a Vector by invoking the iterator or listIterator method.

Amala Rangnekar said: (Jul 17, 2014)  
If 'it is it is also possible to get an Iterator over a Vector by invoking the iterator or listIterator method', then why are the values false?

Navya said: (Aug 6, 2014)  
Ya someone please explain why its false ?

Anadikrishna said: (Sep 8, 2014)  
Because ArrayList Does not implement the Enumerator interface. But Vector implements the Enumerator interface.

Vector doesn't support Iterator and List iterator Interface. So Vector Obj is not an instance of these two.

Awdhesh said: (Jul 31, 2015)  
Any one can write code for true true true for above problem?

Rohit said: (Jun 6, 2016)  
@Anadikrishna. You are wrong.

Object x = new Vector().elements(); // for enumeration. (i.e) elements method is there to invoke enumeration so o/p is true,false,false.

If it was Object x = new Vector().iterator(); //now it's for Iterator i.e through iterator() method to invoke Iterator so o/p would be in this case : false , true, true.

Ssimilarly for ListIterator it should be like:

Object x = new Vector().listIterator() and o/p would be : false ,false ,true.

Raj said: (Oct 27, 2018)  
When we use this -> Object x = new Vector().listIterator().
The output will be -> false,true,true.

Am I right?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.