Welcome Guest | Sign in | Register
Collections - Discussion Page For Q.2565 | Java Programming Questions and Answers | Java Programming Free Online-Tests | LucentBlackBoard | LucentBlackBoard.com

Home > Java Programming > Collections > Questions and Answers

Q: What is the output for the below code?

import java.util.Iterator;
import java.util.TreeSet;
public class Test {
public static void main(String... args) {
TreeSet s1 = new TreeSet();
s1.add("one");
s1.add("two");
s1.add("three");
s1.add("one");
Iterator it = s1.iterator();
while (it.hasNext() ) {
System.out.print( it.next() + " " );
}
}
}
A. one three two B. Runtime Exception
C. one three two one D. one two three

Answer and Explanation

Answer:one three two

Explanation:
The TreeSet assures no duplicate entries. It will return elements in natural order, which for Strings mean alphabetical order.

Comment on this Question

Name:

Loading...




Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved © 2012-2015 SoftLucent.