Menu
Topics Index
...
`

Collections1660


If you need explanation Read this topic

If you need Answer Take test on this topic

Collections1660
What will be the output of the following program?
public class CommonType<E> {
    static E x;
    public static <E> void append(List<E> list, Class<E> cls) throws InstantiationException, IllegalAccessException {
        E elem = cls.newInstance();
        elem = (E) x; // LINE B
        list.add(elem);
    }
    public static void main(String arg[]) throws InstantiationException, IllegalAccessException {
        List<String> list = new ArrayList();
        x = "Welcome";
        append(list, String.class);
        x = "To";
        append(list, String.class);
        x = "Merit";
        append(list, String.class);
        x = "Campus";
        append(list, String.class);
        for (String str : list) { // LINE A
            System.out.print(str);
        }
    }
}

A. WelcomeToMeritCampus
B. Compilation Error
C. Run time Error at LINE A
D. Run time Error at LINE B
E. Runtime Error for some other reason
Topic: Java ArrayList

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.

a

Posted by Somasekhara Kesani    2013-08-02 08:35:52


Option B it must be,coz we cannot make a static reference 'x'' to the non-static type E

Posted by Shaik Nyamath    2013-08-02 09:31:32


ans is D because JVM cannot typecast x because they are of incompatable types there is no relation between them

Posted by Ram Reddy Bitla    2013-08-02 10:48:16


c

Posted by Aruna Dantu    2013-08-02 14:29:15


Congratulations Shaik Nyamath. You are this dose winner. Please send us your mobile details and we will process the recharge asap.

Posted by Merit Campus    2013-08-03 05:14:12


Got RC :)

Posted by Shaik Nyamath    2013-08-03 16:10:38


© meritcampus 2019

All Rights Reserved.

Open In App