What will be the output of the following code ?<br /> <br /> enum Day {<br /> MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY<br /> }<br /> <br /> public class BuggyBread1{<br /> public static void main (String args[]) {<br /> Set mySet = new TreeSet();<br /> mySet.add(Day.SATURDAY);<br /> mySet.add(Day.WEDNESDAY);<br /> mySet.add(Day.FRIDAY);<br /> mySet.add(Day.WEDNESDAY);<br /> for(Day d: mySet){<br /> System.out.println(d);<br /> }<br /> }<br /> }
Javasearch.buggybread.com
Share

Search Interview Questions


 3358 questions in repository.
 There are more than 200 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

   



Java - Interview Questions and Answers

 Q1. What will be the output of the following code ?

enum Day {
MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY
}

public class BuggyBread1{
public static void main (String args[]) {
Set mySet = new TreeSet();
mySet.add(Day.SATURDAY);
mySet.add(Day.WEDNESDAY);
mySet.add(Day.FRIDAY);
mySet.add(Day.WEDNESDAY);
for(Day d: mySet){
System.out.println(d);
}
}
}
Core Java
Admin
info@buggybread.com
Ans. WEDNESDAY
FRIDAY
SATURDAY

Only one FRIDAY will be printed as Set doesn't allow duplicates.Elements will be printed in the order in which constants are declared in the Enum. TreeSet maintains the elements in the ascending order which is identified by the defined compareTo method. compareTo method in Enum has been defined such that the constant declared later are greater than the constants declared prior.

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

   Like         Discuss         Correct / Improve     java   code   coding   enum   set   treeset   advanced

Try 3 Question(s) Test


Related Questions

 Difference beween HashSet and TreeSet ?
  What is comparator interface used for ?
  If I try to add Enum constants to a TreeSet, What sorting order will it use ?
  Which are the sorted collections ?
  If we add Enum constants to a sorted collection ( Treemap , TreeSet ), What will be the order in which they will be maintained ?
 What is the difference between TreeSet and TreeMap ?
 Which of the following is insertion heavy - HashSet or TreeSet ?
 How to sort objects based on one of the field ?




Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus
 

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:
         

X Close this