Menu
Question Index
...

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


WelcomeToMeritCampus
Compilation Error
Run time Error at LINE A
Run time Error at LINE B
Runtime Error for some other reason

Doubts

Problems

Topic: Java ArrayList

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App