Topics Index


If you need explanation Read this topic

If you need Answer Take test on this topic

What will be the output of the following program?
import java.util.*;
public class Master {
    public static void main(String[] args) {
        TreeSet map = new TreeSet();
        Iterator it = map.iterator();
        while (it.hasNext()) {
            System.out.print( + " ");

A. one two three four
B. four three two one
C. four one three two
D. one two three four one
E. Compilation Error or Runtime Error
Topic: Java Iterator

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.

option C..In the treeset strings are stored in ascending order..

Posted by Soumya Tata    2013-06-17 08:07:23

c is answer since using the iterator interface we are traversing until no element is available in the treeset  & values which are duplicates are not printed back.

Posted by Karteek Paruchuri    2013-06-17 08:08:06

Ans is TreeSet doesn't allow duplicates n it stores data in objects natural sorting order

Posted by Krishna Sai    2013-06-17 08:08:28

option c is correct as the treeset class stores the elements in ascending order and it wont take duplicates

Posted by Vijay Kumar    2013-06-17 08:23:01

option c: because TreeSet dont store duplicate values and are stored in increasing order of their ascii values of the first letter of the word out of all, "f" in four has least ascii value and next comes "o" in one and so on.

Posted by Maanassa Sudha    2013-06-17 10:14:07

Congratulations Soumya Tata. You are this dose winner. Please send us your mobile details and we will process the recharge asap.

Posted by Merit Campus    2013-06-17 12:34:56 RC :)

Posted by Soumya Tata    2013-06-28 10:55:21

© meritcampus 2019

All Rights Reserved.

Open In App