Menu
Topics Index
...
`

Collections1772


If you need explanation Read this topic

If you need Answer Take test on this topic

Collections1772
What will be the output of the following program?
import java.util.*;
public class Printer {
    public static void main(String args[]) {
        List<Integer> list = new ArrayList<Integer>();
        list.add(25);    list.add(30);
        list.add(35);    list.add(40);
        list.add(45);
        Set<Integer> s = new TreeSet<Integer>();
        s.add(20);    s.add(35);
        s.add(55);    s.add(35);
        s.add(45);
        for (int i = 0; i < list.size(); i++) {
            if (!s.add(list.get(i)))
                System.out.print(list.get(i) + ",");
        }
        System.out.println("\n" + s);
    }
}

A. 35,45,
[20, 35, 55, 45, 25, 30, 40]
B. 35,45,
[20, 25, 30, 35, 40, 45, 55]
C. 20,25,30,35,40,45,55
[35, 45]
D. Some other output
E. Compilation Error or Runtime Error
Topic: Java TreeSet - TreeSet Examples in Java

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.

ans is c

Posted by Shireesha Lanka    2013-09-13 09:19:21


B is the answer.since in first SOP the same elements of set and list are printed.and in the next SOP all the merged elements of list and set are printed

Posted by Shabaaz Shaikz    2013-09-13 09:20:56


b is the answer here when we try to add to the s we check if the val already exists if it doesn't print it

Posted by Karteek Paruchuri    2013-09-13 09:29:56


B is answer.as matching elements of set and list are checked and printed first.and then combined elements are printed.

Posted by Shaik Shayista    2013-09-13 09:35:25


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

Posted by Merit Campus    2013-09-16 05:22:38


© meritcampus 2019

All Rights Reserved.

Open In App