Menu
Question Index
...

What will be the output of the following program?

import java.util.*;
class Collection<E> {
    List<Integer> myMethod(java.util.Collection<E> input) throws Exception { // LINE A
        List<Integer> result = new ArrayList<Integer>();
        for (E e : input) {
            result.add(Integer.parseInt((e.getClass().getDeclaredField("value").toString())));// LINE B
        }
    return result;
    }
}
class Test {
    private final Integer value;
    private final String name;
    public Integer getValue() { return value; }
    public String getName() {return name; }
    public Test(Integer value, String name) {
        this.name = name;
        this.value = value;
    }
}
public class CollectionTest {
    public static void main(String ar[]) throws Exception {
        Collection<Test> collection = new Collection<Test>();
        List<Test> list = new ArrayList<Test>();
        list.add(new Test(1, "1"));
        list.add(new Test(2, "2"));
        list.add(new Test(3, "3"));
        list.add(new Test(4, "4"));
        list.add(new Test(5, "5"));
        List<Integer> result = collection.myMethod(list);
        for (Integer test : result)
            System.out.print(test + ",");
    }
}


1,2,3,4,5,
Compilation Error at LINE A
Compilation Error at LINE B
throws ClassCastException
throws NumberFormatException

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