Menu
Topics Index
...
`

Collections : Iterator


If you need explanation Read this topic

If you need Answer Take test on this topic

Collections : Iterator
What will be the output of the following program?
import java.lang.reflect.*;
import java.util.*;

public class Reflector {
    public static void main(String[] args) throws Exception {
        Set<String> s = new HashSet<String>();
        s.add("B");
        s.add("A");
        s.add("B");
        s.add("A");
        Iterator it = s.iterator();
        Method m = it.getClass().getMethod("hasNext");
        System.out.println(m.invoke(it));
    }
}

A. B
B. BA
C. BABA
D. Some other output
E. Compilation Error or Runtime Error
Topic: Collection Framework In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

ans is E. Runtime Error

Posted by Ravi Teja    2013-12-06 08:34:12


answer is 'C' getClass method returns object of iterator it's class n it is stored in m so m.invoke(it) prints BABA

Posted by Sai Teja    2013-12-06 08:56:03


D is the answer Hash set arrange String in alphabetical order i.e AABB

Posted by Yeshu Yeshwanthi    2013-12-06 08:59:19


E is the answer.Run time error.Reflector cant access a member of HashIterator with public function and atleast there shld be try catch blocks for Method m and SOP.

Posted by Shabaaz Shaikz    2013-12-06 09:25:07


Congratulations Shabaaz Shaikz. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2013-12-09 03:20:16


:-)

Posted by Shabaaz Shaikz    2013-12-09 05:15:00


© meritcampus 2019

All Rights Reserved.

Open In App