Menu
Question Index
...

What will be the output of the following program?

import java.util.*;
public class AddStrings<T> {
    Integer add(Collection<String> obj) {
        int res = 0;
        for (Object o : obj) {
            res += (Integer) o;
        }
        return res;
    }
    public static void main(String args[]) {
        List<?> numbers = Arrays.asList(1, 2, 3);
        System.out.println("Sum = " + new AddStrings().add(numbers));
    }
}


Sum = 6
Sum = 123
Some other output
Compilation Error or Runtime Error

Doubts

Problems

Topic: Why Collections Are Generic In Java?

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App