Java Programming - Objects and Collections - Discussion
Discussion Forum : Objects and Collections - Finding the output (Q.No. 7)
7.
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);
}
}
Answer: Option
Explanation:
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.
Discussion:
6 comments Page 1 of 1.
Raj said:
7 years ago
When we use this -> Object x = new Vector().listIterator().
The output will be -> false,true,true.
Am I right?
The output will be -> false,true,true.
Am I right?
Rohit said:
9 years ago
@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.
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.
Awdhesh said:
1 decade ago
Any one can write code for true true true for above problem?
AnadiKrishna said:
1 decade ago
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.
Vector doesn't support Iterator and List iterator Interface. So Vector Obj is not an instance of these two.
Navya said:
1 decade ago
Ya someone please explain why its false ?
Amala rangnekar said:
1 decade ago
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?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers