Hashmap and Hashset in Java MCQs Exercise I
Ques 1
Hashmap and Hashset
Which data structure does HashMap use internally to store key-value pairs?
A Array
B LinkedList
C HashSet
D TreeMap
Ques 2
Hashmap and Hashset
What happens if you try to add a duplicate element to a HashSet?
A The new element replaces the old element.
B The duplicate element is ignored and the old element remains.
C It throws an exception.
D It is not allowed to have duplicate elements in a HashSet.
Ques 3
Hashmap and Hashset
What happens if you try to add a duplicate key to a HashMap?
A The new key replaces the old key.
B It throws an exception.
C The duplicate key is ignored and the old key remains.
D It is not allowed to have duplicate keys in a HashMap.
Ques 4
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");
set.clear();
System.out.println(set.size());
A 0
B 1
C 2
D Compilation error
Ques 5
Hashmap and Hashset
What is the output of the following Java Code?
HashMap map = new HashMap<>();
map.put("One", 1);
map.put("Two", 2);
map.put("Three", 3);
System.out.println(map.size());