Java MCQ – Home
Java Basic MCQ
➤ Java Hello World MCQ
➤ Find Java Keywords
➤ Java Identifier Quiz
➤ Java Data Types Quiz
➤ If-Else MCQ in Java-1
➤ If-Else MCQ in Java-2
Object class MCQ
➤ Object Class Quiz
➤ equals() Method Quiz
➤ Hashcode Value Quiz
➤ toString() Method Quiz
➤ Clone() Method Quiz
Multithreading MCQ
➤ Define a Thread-1
➤ Define a Thread-2
➤ Get/set ThreadName
➤ Thread State MCQ
➤ Thread Priority MCQ
➤ Yield(), join() & sleep()
➤ Synchronization MCQ
➤ Interthread Comms
➤ Deadlock, Daemon
Exception Handling
➤ Exception Handling-1
➤ Exception Handling-2
➤ Exception Handling-3
➤ Java try-catch MCQ-1
➤ Java try-catch MCQ-2
➤ Java try-catch MCQ-3
➤ Nested try-catch MCQ
➤ throw Keyword MCQ
➤ finally Block MCQ-1
➤ finally Block MCQ-2
➤ throws Keyword MCQ
Generics MCQ
➤ Java Generics Quiz-1
➤ Java Generics Quiz-2
➤ Java Generics Quiz-3
Collection Framework
➤ Collections Quiz-1
➤ Collections Quiz-2
➤ ArrayList MCQ-1
➤ ArrayList MCQ-2
➤ LinkedList MCQ
➤ Vector Stack MCQ
➤ Java Cursors MCQ
➤ Java TreeSet MCQ
➤ TreeSet & Comparator
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 Overview, Java Collection Interface, List 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 TreeIn 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, falseIf 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) FalseIn 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) NullPointerExceptionIn 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) ClassCastExceptionIn 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) NullPointerExceptionFrom 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) ClassCastExceptionIf 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 runtimeIn 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!