public class BuggyBread1{<br /> <br /> public static void main (String args[]) {<br /> Set<String> mySet = new TreeSet<String>();<br /> mySet.add(""1"");<br /> mySet.add(""2"");<br /> mySet.add(""111"");<br /> for(String d: mySet){<br /> System.out.println(d);<br /> }<br /> }<br /> }
Javasearch.buggybread.com

Search Interview Questions


 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Interview Questions and Answers

 Q1. public class BuggyBread1{

public static void main (String args[]) {
Set<String> mySet = new TreeSet<String>();
mySet.add(""1"");
mySet.add(""2"");
mySet.add(""111"");
for(String d: mySet){
System.out.println(d);
}
}
}
Core Java
Ans. 1
111
2

TreeSet maintains the elements in the ascending order which is identified by the compareTo method. compareTo method in String has been defined such that it results in the natural alphabetic Order. Here the elements in the TreeSet are of String and not of Integer. In String Natural Order, 111 comes before 2 as ascii of 1st character first determines the order.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   code   coding   set   treeset   string   integer


Related Questions

  Difference between == and .equals() ?
  Why is String immutable in Java ?
  Explain the scenerios to choose between String , StringBuilder and StringBuffer ?

or

What is the difference between String , StringBuilder and StringBuffer ?
  Why Char array is preferred over String for storing password?
  What are different ways to create String Object? Explain.
  What is a String Pool ?
 How does making string as immutable helps with securing information ? How does String Pool pose a security threat ?
 How is string object immutable if we can concat a string to it ?
  What is StringJoiner ?
 Which of the following is false about main method ?

a. It should be declared public and static
b. it should have only 1 argument of type String array
c. We can override main method
d. We can overload main method



Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: