Menu
Topics Index
...
`

Generic Class Method and Variables


If you need explanation Read this topic

If you need Answer Take test on this topic

Generic Class Method and Variables
What will be the output of the following program?
import java.util.*;

public class Competition<T> {
    String glom(Collection<?> obj) {
        String result = "";
        for (Object o : obj)
            result += o;
        return result;
    }
    int glom(List<Integer> ints) {
        int result = 0;
        for (int i : ints)
            result += i;
        return result;
    }
    public static void main(String args[]) {
        List<String> strings = Arrays.asList("1", "2", "3");
        System.out.println(new Competition().glom(strings));
    }
}

A. 6
B. 123
C. Some other output
D. Output can not be determined
E. Compilation Error or Runtime Error
Topic: java.util.Arrays - Class Arrays In Collection Framework

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.

B is the Answer.. we r passing strings as list to glom function so it will collection of strings.. 1+2+3=123..

Posted by Siddharth Sid    2013-11-29 08:59:51


ans should be E runtime error bcoz we r using for(int i : ints) , here ints is a list of integers and not an array of integers so it must give classcastError...

Posted by Pramod Jain    2013-11-29 10:47:09


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

Posted by Merit Campus    2013-12-02 04:04:16


thank u mc :) got rc :) :)

Posted by Pramod Jain    2013-12-03 04:34:07


© meritcampus 2019

All Rights Reserved.

Open In App