Menu
Topics Index
...
`

TreeSet


If you need explanation Read this topic

If you need Answer Take test on this topic

TreeSet
What will be the output of following program?
import java.util.*;
public class Tree {
    public static void main(String[] args) {
        TreeSet<Integer> tree1 = new TreeSet<Integer>();
        TreeSet<Integer> tree2 = new TreeSet<Integer>();
        for (int i = 100; i < 150; i++)
            if (i % 7 == 0)
                tree1.add(i);
        tree2 = (TreeSet) tree1.subSet(126, false, 134, true);
        tree1.add(176);
        System.out.println(tree1 + " " + tree2);
    }
}

A. [105, 112, 119, 126, 133, 140, 147, 176] [133]
B. [105, 112, 119, 126, 133, 140, 147, 176] [134]
C. [105, 112, 119, 126, 133, 140, 147, 176] [126,134]
D. [105, 112, 119, 126, 133, 140, 147, 176] [125,133]
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.

TreeSet holds the values in ascending order
tree1 holds the multiples of 7 ranging between 100 and 150 i.e 105 112...147
and also 176
tree2 contains the subset of values contained in tree btwn the range 126-134
therefore, tree2 holds 133

OPtion A

Posted by Kittu Manoj    2014-06-03 08:55:29


Answer : A
the tree1 will contain the numbers which are divisble by 7 from 100 to 149
the tree2 will have a subset of tree1 which will contain values starting from 127 as 134  as 126 is not being included

Posted by Deepak Kumar Yadav    2014-06-03 08:57:19


ans a. tree1 add mod 7 values between 100 and 150 ,
and add value 176 .
tree 2 select mod 7  value btwn 126 and 134,
126,133 two values thr but 126 false so tree 2 hve ly 1 value 133

Posted by Maheshwari Natarajan    2014-06-03 08:59:58


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

Posted by Merit Campus    2014-06-04 04:50:45


© meritcampus 2019

All Rights Reserved.

Open In App