Hashmap and Hashset in Java MCQs Exercise II

Ques 1 Hashmap and Hashset


Which of the following statements about HashMap is true?

A It allows duplicate keys.
B It allows null keys but not null values.
C It maintains the insertion order of elements.
D It is a synchronized class.

Ques 2 Hashmap and Hashset


Which of the following is not a method of the HashMap class?

A get()
B remove()
C contains()
D size()

Ques 3 Hashmap and Hashset


What is the time complexity of adding an element to a HashSet?

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

Ques 4 Hashmap and Hashset


Which of the following is not a valid way to iterate over the key-value pairs of a HashMap?

A Using a for-each loop
B Using the values() method
C Using the keys() method
D Using an iterator

Ques 5 Hashmap and Hashset


What is the output of the following Java Code?

HashSet set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Cherry");
System.out.println(set.isEmpty());

A true
B false
C Apple, Banana, Cherry
D Compilation error