Which is the Best Java Web Framework ?

Java - Collections - Interview Questions and Answers on TreeSet

Q1.  What is comparator interface used for ?

Ans. The purpose of comparator interface is to compare objects of the same class to identify the sorting order. Sorted Collection Classes ( TreeSet, TreeMap ) have been designed such to look for this method to identify the sorting order, that is why class need to implement Comparator interface to qualify its objects to be part of Sorted Collections.

Q2.  Name some sorted collections ?

Ans. TreeSet and TreeMap

Q3.  If I try to add Enum constants to a TreeSet, What sorting order will it use ?

Ans. Tree Set will sort the Values in the order in which Enum constants are declared.

Q4.  What will be the output of this code ?

Set<String> mySet = new TreeSet<String>();
mySet.add("4567");
mySet.add("5678");
mySet.add("6789");
for(String s: mySet){
    System.out.println(s);
}

Ans. 

4567
5678
6789

Q5.  enum Day {

 MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY
}

public class BuggyBread1{
    public static void main (String args[]) {
     Set<Day> mySet = new TreeSet<Day>();
     mySet.add(Day.SATURDAY);
     mySet.add(Day.WEDNESDAY);
     mySet.add(Day.FRIDAY);
     mySet.add(Day.WEDNESDAY);
     for(Day d: mySet){
      System.out.println(d);
     }
    }
}

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. 

Q6.  

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);
     }
    }
}

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.

Q7.  

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

Ans. 1
2
111

TreeSet maintains the elements in the ascending order which is identified by the compareTo method. compareTo method in Integer has been defined such that it results in the natural numerical Order.

Q8. Which is the Parent Class of TreeSet class?

Ans.java.util.AbstractSet<E>

Q9. What is the package name for TreeSet class?

Ans.java.util

Q10. Which interfaces are implemented by TreeSet?


Ans.[, Serializable, Set<E>, NavigableSet<E>, SortedSet<E>, Cloneable, Collection<E>, Iterable<E>]

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




×

Try our new Java Interview Questions Search Tool.

There are more than 1000 questions with practice tests.

It's updated daily and It's Awesome.


×

Try our new Java Practice Test tool.

There are more than 200 questions and expanding quickly. It's updated daily and It's Awesome.