Search Tutorials


Java TreeSet MCQ Questions and Answers | JavaInUse

Java TreeSet MCQ Questions and Answers

Q. What is the data structure used to implement the LinkedTreeSet in Java?

A. Linked List
B. Array
C. Balanced Binary Search Tree
D. Hash Table

Q. Which interface does LinkedTreeSet implement in Java?

A. Set
B. List
C. Queue
D. Deque

Q. What is the time complexity of adding an element to a LinkedTreeSet in Java?

A. O(1)
B. O(log n)
C. O(n)
D. O(n log n)

Q. What is the result of calling linkedTreeSet.add(10) if linkedTreeSet contains [5, 8, 15, 3]?

A. [5, 8, 10, 15, 3]
B. [5, 8, 15, 10, 3]
C. [5, 8, 15, 3, 10]
D. [3, 5, 8, 10, 15]

Q. How do you iterate over the elements in a LinkedTreeSet?

A. Using a for loop
B. Using an iterator
C. Using a while loop
D. All of the above

Q. What is the output of the following code snippet?
LinkedTreeSet<String> colors = new LinkedTreeSet<>(List.of("red", "green", "blue"));
colors.remove("green");
System.out.println(colors);

A. [red, blue]
B. [blue, red]
C. [green, red, blue]
D. [blue, green, red]

Q. What is the time complexity of checking if an element exists in a LinkedTreeSet?

A. O(1)
B. O(log n)
C. O(n)
D. O(n log n)

Q. How do you create an empty LinkedTreeSet in Java?

A. LinkedTreeSet<Type> set = new LinkedTreeSet<>();
B. LinkedTreeSet<Type> set = new LinkedTreeSet<Type>();
C. LinkedTreeSet<Type> set = new LinkedTreeSet();
D. LinkedTreeSet set = new LinkedTreeSet();

Q. What is the result of calling linkedTreeSet.ceiling(8) if linkedTreeSet contains [5, 9, 12, 15]?

A. 5
B. 9
C. 12
D. null

Q. What is the output of the following code snippet?
LinkedTreeSet<Integer> numbers = new LinkedTreeSet<>(List.of(1, 2, 3, 4, 5));
numbers.descendingSet().forEach(System.out::println);

A. 5 4 3 2 1
B. 1 2 3 4 5
C. 1 3 5 2 4
D. 5 3 1 4 2





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

TreeSet<String> treeSet = new TreeSet<>();
treeSet.add("Apple");
treeSet.add("Banana");
treeSet.add("Orange");
treeSet.add("Grapes");
for (String fruit : treeSet) {
System.out.print(fruit + " ");
}
A.
Apple Banana Grapes Orange
B.
Apple Banana Orange Grapes
C.
Apple Orange Banana Grapes
D.
Banana Apple Orange Grapes

Q. Which of the following statements about TreeSet is true?

A.

TreeSet allows duplicate elements.

B.

TreeSet does not allow null elements.

C.

TreeSet maintains the insertion order of elements.

D.

TreeSet is not sorted.

Q. How can you create a TreeSet that sorts elements based on their lengths (for strings) or numerical values (for numbers) in descending order?

A.
TreeSet<String> treeSet = new TreeSet<>((a, b) -> b.length() - a.length());
B.
TreeSet<String> treeSet = new TreeSet<>(Comparator.comparing(String::length).reversed());
C.
TreeSet<String> treeSet = new TreeSet<>(Collections.reverseOrder());
D.
TreeSet<String> treeSet = new TreeSet<>((a, b) -> a.compareTo(b));

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

TreeSet<Integer> treeSet = new TreeSet<>();
treeSet.add(3);
treeSet.add(1);
treeSet.add(4);
treeSet.add(2);
for (int num : treeSet) {
System.out.print(num + " ");
}
A.
1 2 3 4
B.
3 4 2 1
C.
3 1 4 2
D.
4 3 2 1

Q. How can you remove a specific element from a TreeSet?

A.
treeSet.remove(element);
B.
treeSet.delete(element);
C.
treeSet.removeElement(element);
D.
treeSet.clear();

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

TreeSet<String> treeSet = new TreeSet<>();
treeSet.add("Apple");
treeSet.add("apple");
for (String fruit : treeSet) {
System.out.print(fruit + " ");
}
A.
Apple
B.
Apple apple
C.
apple Apple
D.
Error: Duplicate element

Q. How can you create a TreeSet that contains custom objects and sorts them based on a specific property?

A.
TreeSet<MyClass> treeSet = new TreeSet<>(Comparator.comparing(MyClass::getProperty));
B.
TreeSet<MyClass> treeSet = new TreeSet<>();
C.
TreeSet<MyClass> treeSet = new TreeSet<>((a, b) -> a.getProperty().compareTo(b.getProperty()));
D.
TreeSet<MyClass> treeSet = new TreeSet<>((a, b) -> b.getProperty().compareTo(a.getProperty()));

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

TreeSet<Integer> treeSet = new TreeSet<>();
treeSet.add(5);
treeSet.add(2);
treeSet.add(8);
treeSet.add(1);
System.out.println(treeSet.first());
System.out.println(treeSet.last());
A.
1
8
B.
5
8
C.
2
5
D.
1
5

Q. How can you check if a TreeSet is empty?

A.
if (treeSet.isEmpty()) {...}
B.
if (treeSet.size() == 0) {...}
C.
if (!treeSet.contains(null)) {...}
D.
if (treeSet.first() == null) {...}

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

TreeSet<String> treeSet = new TreeSet<>();
treeSet.add("Cherry");
treeSet.add("Apple");
treeSet.add("Banana");
System.out.println(treeSet.lower("Apple"));
System.out.println(treeSet.higher("Apple"));
A.
Banana
Cherry
B.
null
Cherry
C.
Banana
null
D.
Apple
Cherry