Question 1
Which of the following statements about Set in Java is correct?
Set allows duplicate elements
Set maintains insertion order
Set does not allow duplicate elements
Set allows duplicate elements but ignores null values
Question 2
Which of the following describes how HashSet stores elements in Java?
It uses a balanced binary tree to store elements.
It stores elements in a hash table using their hash codes.
It stores elements in a linked list for fast insertion.
It uses a stack-based structure for element management.
Question 3
What will be the output of the following code?
import java.util.*;
public class Test {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("Java");
set.add("Python");
set.add("Java");
System.out.println(set);
}
}
[Java, Python, Java]
[Java, Python]
[Python, Java, Java]
Compilation Error
Question 4
What is the time complexity of contains() operation in HashSet?
O(1)
O(n)
O(log n)
O(n log n)
Question 5
What will be the output of the following code?
import java.util.*;
public class Test {
public static void main(String[] args) {
Set<Integer> set = new TreeSet<>();
set.add(5);
set.add(3);
set.add(8);
set.add(1);
System.out.println(set);
}
}
[5, 3, 8, 1]
[1, 3, 5, 8]
[8, 5, 3, 1]
Compilation Error
Question 6
What is the key difference between LinkedHashSet and HashSet?
LinkedHashSet maintains insertion order, while HashSet does not
HashSet is synchronized, while LinkedHashSet is not
LinkedHashSet allows duplicate elements, while HashSet does not
LinkedHashSet is slower than HashSet for all operations
Question 7
What happens when you insert null in a TreeSet?
null is allowed and stored
null is ignored
It throws NullPointerException
TreeSet converts null to an empty string
Question 8
What will be the output of the following code?
import java.util.*;
public class Test {
public static void main(String[] args) {
Set<String> set = new LinkedHashSet<>();
set.add("C");
set.add("Java");
set.add("Python");
set.add("C");
System.out.println(set);
}
}
[C, Java, Python, C]
[Java, Python, C]
[C, Java, Python]
Compilation Error
Question 9
What is the main characteristic of the Set interface in Java?
Maintains insertion order
Does not allow duplicate elements
Allows duplicate elements
Stores elements in key-value pairs
There are 10 questions to complete.