Search Tutorials


Java HashSet MCQ Questions and Answers | JavaInUse

Java HashSet MCQ Questions and Answers

Q. What is the purpose of the HashSet class in Java?

A. To store a collection of unique elements
B. To sort elements in ascending order
C. To provide constant-time access to elements
D. To implement a stack data structure

Q. Which interface does HashSet implement in Java?

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

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

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

Q. How does HashSet handle duplicate elements during addition?

A. It adds the duplicate element
B. It replaces the existing element
C. It throws an exception
D. It ignores the duplicate element

Q. What is the time complexity of removing an element from a HashSet in Java?

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

Q. How does HashSet handle null elements?

A. It allows multiple null elements
B. It allows only one null element
C. It throws an exception for null elements
D. It treats null as a regular element

Q. What is the default load factor of a HashSet in Java?

A. 0.5
B. 0.75
C. 1.0
D. It varies based on the JVM

Q. How can you iterate over the elements of a HashSet in Java?

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 new HashSet<>(List.of(1, 2, 2, 3, 4)).size() in Java?

A. 4
B. 5
C. It throws an exception
D. It depends on the JVM

Q. How can you create a HashSet that is synchronized and thread-safe in Java?

A. By using the Collections.synchronizedSet() method
B. By extending the HashSet class and overriding its methods
C. By using the ConcurrentHashMap class
D. HashSet cannot be made thread-safe

Q. What is the output of the following code?

HashSet<String> colors = new HashSet<>();
colors.add("Red");
colors.add("Green");
colors.add("Blue");
colors.add("Red");
for (String color : colors) {
System.out.println(color);
}
A.
Red
Green
Blue
B.
Red
Green
C.
Green
Blue
Red
D.
Blue
Red
Green





Q. Which interface must be implemented by classes that want to store their elements in a HashSet?

A. Comparable B. Cloneable C. Serializable D. Iterable

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

HashSet<Integer> numbers = new HashSet<>();
numbers.add(3);
numbers.add(1);
numbers.add(5);
numbers.add(4);
numbers.add(2);
numbers.add(3);
System.out.println(numbers);
A.
[1, 2, 3, 4, 5]
B.
[3, 1, 5, 4, 2]
C.
[3, 1, 4, 5, 2]
D.
[3, 5, 1, 4, 2]

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

A.
HashSet guarantees the order of insertion.
B.
HashSet allows duplicate elements.
C.
HashSet is an implementation of SortedSet.
D.
HashSet uses a hash table and equals method for storage.

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

HashSet<String> fruits = new HashSet<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.remove("Banana");


System.out.println(fruits);
A.
[Apple, Orange]
B.
[Banana, Orange]
C.
[Apple, Banana]
D.
[Orange, Apple]

Q. How can you create an empty HashSet of type Integer?

A.
HashSet<Integer> set = new HashSet<>();
B.
HashSet<Integer> set = new HashSet();
C.
HashSet<Integer> set = new HashSet<Integer>();
D.
HashSet set = new HashSet<Integer>();

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

HashSet<String> set = new HashSet<>();
set.add("Hello");
set.add("World");
set.addAll(List.of("Java", "Python", "Hello"));


System.out.println(set);
A.
[Hello, World, Java, Python]
B.
[Hello, World, Java]
C.
[Hello, World, Python]
D.
[Hello, World, Java, Python, null]

Q. Which of the following methods is used to get the size of a HashSet?

A. length() B. count() C. size() D. getTotal()

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

HashSet<String> languages = new HashSet<>(List.of("Java", "Python", "C++"));
System.out.println(languages);
A.
[Java, Python, C++]
B.
[Python, C++, Java]
C.
[C++, Java, Python]
D.
[Python, Java, C++]

Q. Which of the following methods is used to iterate over the elements of a HashSet?

A. forEach() B. iterate() C. loop() D. traverse()