Java TreeSet MCQ

Java TreeSet 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 MCQ and programming questions, answer them. See:- Java Collections Quiz-1

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

Q1) In TreeSet the underlying data structure is?

a) Hashtable
b) Balanced Tree
c) UnBalanced Tree
d) AVL Tree

View Answer Answer:- b) Balanced Tree

In TreeSet, the underlying data structure is the balanced tree.

Q2) Find the output of the below program?

import java.util.*;
public class Test {
   public static void main(String[] args) {
      TreeSet<String> ts = new TreeSet<String>();
      ts.add("A");
      ts.add("A");
      System.out.println(ts);
   }
}

a) [A]
b) [A, A]
c) Compile time error
d) Compiled Successfully but Exception is raised at runtime

View Answer Answer:- a)[A]

By default, TreeSet doesn’t allow inserting duplicate values.

Q3) What will be the output of the below program?

import java.util.*;
public class Test {
   public static void main(String[] args) {
      TreeSet<String> ts = new TreeSet<String>();
      System.out.print(ts.add("A") + ", ");
      System.out.print(ts.add("A"));
   }
}

a) true, true
b) true, false
c) false, true
d) false, false

View Answer Answer:- b) true, false

If we try to store duplicate value to the TreeSet then add() method returns false, else for non-duplicate values add() method returns true.

Q4) Insertion order is preserved in TreeSet?

a) Ture
b) False

View Answer Answer:- b) False

In TreeSet, insertion order is not preserved.

Q5) Which option is correct for the below program?

import java.util.*;
public class Test {
   public static void main(String[] args) {
      TreeSet<String> ts = new TreeSet<String>();
      ts.add("A");
      ts.add(null);
      System.out.println(ts);
   }
}

a) Compile time error
b) NullPointerException
c) ClassCastException
d) Compiled Successfully, Run Successfully, and output is displayed on the screen.

View Answer Answer:- b) NullPointerException

In TreeSet, null insertion is not possible else we will get NullPointerException.

Q6) Which option is correct for the below program?

import java.util.*;
public class Test {
   public static void main(String[] args) {
      TreeSet ts = new TreeSet();
      ts.add("A");
      ts.add(10);
      System.out.println(ts);
   }
}

a) Compile time error
b) NullPointerException
c) ClassCastException
d) Compiled Successfully, Run Successfully, and output is displayed on the screen.

View Answer Answer:- c) ClassCastException

In TreeSet, Heterogeneous objects are not allowed otherwise we will get runtime exception:- ClassCastException. On, calling ts.add(10) JVM internally calls ((Integer)10).compareTo(“A”). String and Integer are incomparable, and java.lang.String cannot be cast to java.lang.Integer class. Hence it gives ClassCastException.

Q7) Which option is correct for the below program?

import java.util.*;
public class Test {
   public static void main(String[] args) {
      TreeSet<String> ts = new TreeSet<String>();
      ts.add(null);
      System.out.println(ts);
   }
}

a) Compile time error
b) NullPointerException
c) ClassCastException
d) Compiled Successfully, Run Successfully, and output is displayed on the screen.

View Answer Answer:- b) NullPointerException

From Java 1.7 version onwards, in TreeSet null insertion is not possible even as the first element. It gives NullPointerException.

Q8) Which option is correct for the below program?

import java.util.*;
public class Test {
   public static void main(String[] args) {
      TreeSet<A> ts = new TreeSet<A>();
      ts.add(new A("A"));
      ts.add(new A("Z"));
      ts.add(new A("L"));
      System.out.println(ts);
   }
}

class A {
   String a;
   A(String a) {
      this.a = a;
   }
}

a) Compile time error
b) NullPointerException
c) ClassCastException
d) Compiled Successfully, Run Successfully, and output is displayed on the screen.

View Answer Answer:- c) ClassCastException

If we are depending on default natural sorting order then compulsory the object should be homogeneous & comparable type otherwise it gives runtime exception:- ClassCastException.

An object is said to be comparable if & only if the corresponding class implements the Comparable interface. For example:- String, StringBuffer, StringBuilder, all Wrapper classes already implements Comparable interface.

The above program gives:- Exception in thread “main” java.lang.ClassCastException: class A cannot be cast to class java.lang.Comparable (A is in unnamed module of loader ‘app’; java.lang.Comparable is in module java.base of loader ‘bootstrap’).

Q9) Find the output of the below program?

import java.util.*;
public class Test {
   public static void main(String[] args) {
      TreeSet<String> ts = new TreeSet<String>();
      ts.add("a");
      ts.add("A");
      ts.add("Z");
      ts.add("c");
      ts.add("K");
      System.out.println(ts);
   }
}

a) [A, K, Z, a, c]
b) [c, a, Z, K, A]
c) [a, A, Z, c, K]
d) [K, c, Z, A, a]

View Answer Answer:- a) [A, K, Z, a, c]

Before sorting values to the TreeSet JVM internally calls compareTo() method of String class because we are storing String type values. In String class compareTo() method is overridden for lexicographical order. Hence values will be inserted according to lexicographical order. In lexicographical order, uppercase case letters come first compared to lower case letters because of their ASCII value.

Q10) Find the output of the below program?

import java.util.*;
public class Test {
   public static void main(String[] args) {
      TreeSet<StringBuilder> ts = new TreeSet<StringBuilder>();
      ts.add(new StringBuilder("a"));
      ts.add(new StringBuilder("A"));
      ts.add(new StringBuilder("Z"));
      ts.add(new StringBuilder("c"));
      ts.add(new StringBuilder("K"));
      System.out.println(ts);
   }
}

a) [A, K, Z, a, c]
b) [c, a, Z, K, A]
c) [a, A, Z, c, K]
d) [K, c, Z, A, a]

View Answer Answer:- a) [A, K, Z, a, c]

Similar to the Sting class, StringBuffer & StringBuilder classes also implement Comparable interface and implement compareTo() method for lexicographical order.

Q11) Find the output of the below program?

import java.util.*;
public class Test {
   public static void main(String[] args) {
      TreeSet ts = new TreeSet();
      ts.add("a");
      ts.add("A");
      ts.add(new StringBuilder("Z"));
      ts.add(new StringBuilder("c"));
      ts.add(new StringBuilder("K"));
      System.out.println(ts);
   }
}

a) [A, K, Z, a, c]
b) [c, a, Z, K, A]
c) Compile time error
d) Exception at runtime

View Answer Answer:- d) Exception at runtime

In the first 2 add() methods we are adding String type value, and in the next lines, we are trying to add StringBuilder type value. Both String and StringBuilder are incompatible types. Hence here we get:-

Exception in thread “main” java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.StringBuilder.

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

Leave a Comment

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