Java TreeSet & Comparator MCQ

Java TreeSet & Comparator MCQ | In Java, collection framework classes are used to store elements in value or key-value format without size limitation. We have listed some Java TreeSet & Comparator MCQ and programming questions, answer them. See:- Java Collections Quiz-1

Also see- Java Collection Framework OverviewJava Collection InterfaceList Interface in Java

Q1) Find the output of the below Java program?

import java.util.*;

public class Test {
   public static void main(String[] args) {

      TreeSet<Integer> ts = new TreeSet<Integer>();
      ts.add(100);
      ts.add(0);
      ts.add(150);
      ts.add(50);
      ts.add(125);
      ts.add(125);

      System.out.println(ts);
   }
}

a) [150, 125, 100, 50, 0]
b) [0, 50, 100, 125, 150]
c) [100, 0, 150, 50, 125]
d) [100, 0, 150, 50, 125, 125]


Answer:- b) [0, 50, 100, 125, 150]

To add values to the TreeSet, JVM will internally call the compareTo() method. In Integer class compareTo() method is overridden for ascending order, therefore these values will be inserted in ascending order. Set doesn’t allow duplicate values.


The program used within questions 2 to 9:-

import java.util.*;

public class Test {
   public static void main(String[] args) {

      TreeSet<Integer> ts = 
           new TreeSet<Integer>(new MyComparator());
      ts.add(100);
      ts.add(0);
      ts.add(150);
      ts.add(50);
      ts.add(125);
      ts.add(125);

      System.out.println(ts);
   }
}

Q2) Find the output of the above Test class if we develop MyComparator class as below?

class MyComparator implements Comparator<Integer> {
   @Override
   public int compare(Integer i1, Integer i2) {
      if (i1 < i2) return +1;
      else if (i1 > i2) return -1;
      else return 0;
   }
}

a) [150, 125, 100, 50, 0]
b) [0, 50, 100, 125, 150]
c) [100, 0, 150, 50, 125]
d) [100, 0, 150, 50, 125, 125]

PrepBytes
Use Code KNOWPROGRAM10 to get FLAT 10% OFF on all Courses


Answer:- a) [150, 125, 100, 50, 0]

In this program, we are using constructor:- “TreeSet<integer>(new MyComparator())”. JVM will call compare() method which is meant for customized sorting. In MyComparator class, we developed compare() method for descending sorting order purpose.


Q3) Find the output of the previous Test class if we develop MyComparator class as below?

class MyComparator implements Comparator<Integer> {
   @Override
   public int compare(Integer i1, Integer i2) {
      return i1.compareTo(i2);
   }
}

a) [150, 125, 100, 50, 0]
b) [0, 50, 100, 125, 150]
c) [100, 0, 150, 50, 125]
d) [100, 0, 150, 50, 125, 125]


Answer:- b) [0, 50, 100, 125, 150]

We developed MyComparator class for customized sorting but in the compare() method it is calling compareTo() method & in Integer class compareTo() method is overridden for default natural sorting order i.e. ascending order.


Q4) Find the output of the previous Test class if we develop MyComparator class as below?

class MyComparator implements Comparator<Integer> {
   @Override
   public int compare(Integer i1, Integer i2) {
      return -(i1.compareTo(i2));
   }
}

a) [150, 125, 100, 50, 0]
b) [0, 50, 100, 125, 150]
c) [100, 0, 150, 50, 125]
d) [100, 0, 150, 50, 125, 125]


Answer:- a) [150, 125, 100, 50, 0]

It is similar to the previous question, but in compare() method it returns the reverse of the compareTo() method return value. Hence it reverses the overall sorting order and elements will be inserted in descending order.


Q5) Find the output of the previous Test class if we develop MyComparator class as below?

class MyComparator implements Comparator<Integer> {
   @Override
   public int compare(Integer i1, Integer i2) {
      return i2.compareTo(i1);
   }
}

a) [150, 125, 100, 50, 0]
b) [0, 50, 100, 125, 150]
c) [100, 0, 150, 50, 125]
d) [100, 0, 150, 50, 125, 125]


Answer:- a) [150, 125, 100, 50, 0]

When we were calling i1.compareTo(i2) then the compareTo() method was called on the i1 method and compared with the i2 object. In that case, we get ascending order. But now the compareTo() method is called on i2 object and compared with i1 object which is exactly reverse of the previous case. Hence we get descending order.


Q6) Find the output of the previous Test class if we develop MyComparator class as below?

class MyComparator implements Comparator<Integer> {
   @Override
   public int compare(Integer i1, Integer i2) {
      return -(i2.compareTo(i1));
   }
}

a) [150, 125, 100, 50, 0]
b) [0, 50, 100, 125, 150]
c) [100, 0, 150, 50, 125]
d) [100, 0, 150, 50, 125, 125]


Answer:- b) [0, 50, 100, 125, 150]

The compare() method returns the reverse of the return value of compareTo() method. Hence we get ascending order.


Q7) Find the output of the previous Test class if we develop MyComparator class as below?

class MyComparator implements Comparator<Integer> {
   @Override
   public int compare(Integer i1, Integer i2) {
      return +1;
   }
}

a) [0, 50, 100, 125, 150]
b) [100, 0, 150, 50, 125]
c) [100, 0, 150, 50, 125, 125]
d) [125, 125, 50, 150, 0, 100]


Answer:- c) [100, 0, 150, 50, 125, 125]

Since compare() method is not comparing values, and simply returning +1 therefore elements will be stored in their insertion order (as they are inserted into the TreeSet). Only those values will be considered as a duplicate for which compare() method return 0 but in this compare() method 0 is never returned hence any of these values will not be considered as duplicate, 125 will be added twice.


Q8) Find the output of the previous Test class if we develop MyComparator class as below?

class MyComparator implements Comparator<Integer> {
   @Override
   public int compare(Integer i1, Integer i2) {
      return -1;
   }
}

a) [100]
b) [125]
c) [100, 0, 150, 50, 125, 125]
d) [125, 125, 50, 150, 0, 100]


Answer:- d) [125, 125, 50, 150, 0, 100]

Compared to the previous question it is returning -1. Hence elements will be sorted in the reverse of the insertion order.


Q9) Find the output of the previous Test class if we develop MyComparator class as below?

class MyComparator implements Comparator<Integer> {
   @Override
   public int compare(Integer i1, Integer i2) {
      return 0;
   }
}

a) [100]
b) [125]
c) [100, 0, 150, 50, 125, 125]
d) [125, 125, 50, 150, 0, 100]


Answer:- a) [100]

The first element is stored as usual. But second elements onwards, each element will be compared with existing stored elements & every time compare() method returns 0. Hence all of them will be considered as duplicate elements.


If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Mern Stack Web Development
Use Code KNOWPROGRAM10 to get FLAT 10% OFF on all Courses

Leave a Comment

Your email address will not be published. Required fields are marked *