250+ TOP MCQs on Data Structures-HashMap and Answers

This set of Java Multiple Choice Questions & Answers (MCQs) on “Data Structures-HashMap”.

1. Map implements collection interface?
a) True
b) False

Answer: b
Clarification: Collection interface provides add, remove, search or iterate while map has clear, get, put, remove, etc.

2. Which of the below does not implement Map interface?
a) HashMap
b) Hashtable
c) EnumMap
d) Vector

Answer: d
Clarification: Vector implements AbstractList which internally implements Collection. Others come from implementing the Map interface.

3. What is the premise of equality for IdentityHashMap?
a) Reference equality
b) Name equality
c) Hashcode equality
d) Length equality

Answer: a
Clarification: IdentityHashMap is rarely used as it violates the basic contract of implementing equals() and hashcode() method.

4. What happens if we put a key object in a HashMap which exists?
a) The new object replaces the older object
b) The new object is discarded
c) The old object is removed from the map
d) It throws an exception as the key already exists in the map

Answer: a
Clarification: HashMap always contains unique keys. If same key is inserted again, the new object replaces the previous object.

5. While finding the correct location for saving key value pair, how many times the key is hashed?
a) 1
b) 2
c) 3
d) unlimited till bucket is found

Answer: b
Clarification: The key is hashed twice; first by hashCode() of Object class and then by internal hashing method of HashMap class.

6. Is hashmap an ordered collection.
a) True
b) False

Answer: b
Clarification: Hashmap outputs in the order of hashcode of the keys. So it is unordered but will always have same result for same set of keys.

7. If two threads access the same hashmap at the same time, what would happen?
a) ConcurrentModificationException
b) NullPointerException
c) ClassNotFoundException
d) RuntimeException

Answer: a
Clarification: The code will throw ConcurrentModificationException if two threads access the same hashmap at the same time.

8. How to externally synchronize hashmap?
a) HashMap.synchronize(HashMap a);
b)

HashMap a = new HashMap();
a.synchronize();

c) Collections.synchronizedMap(new HashMap());
d) Collections.synchronize(new HashMap());

Answer: c
Clarification: Collections.synchronizedMap() synchronizes entire map. ConcurrentHashMap provides thread safety without synchronizing entire map.

9. What will be the output of the following Java code snippet?

  1. public class Demo
  2. {
  3.   public static void main(String[] args)
  4.   {
  5. 		Map<Integer, Object> sampleMap = new TreeMap<Integer, Object>();
  6. 		sampleMap.put(1, null); 
  7. 		sampleMap.put(5, null); 
  8. 		sampleMap.put(3, null); 
  9. 		sampleMap.put(2, null); 
  10. 		sampleMap.put(4, null); 
  11.  
  12.        System.out.println(sampleMap);
  13.    }
  14. }

a) {1=null, 2=null, 3=null, 4=null, 5=null}
b) {5=null}
c) Exception is thrown
d) {1=null, 5=null, 3=null, 2=null, 4=null}

Answer: a
Clarification: HashMap needs unique keys. TreeMap sorts the keys while storing objects.

10. If large number of items are stored in hash bucket, what happens to the internal structure?
a) The bucket will switch from LinkedList to BalancedTree
b) The bucket will increase its size by a factor of load size defined
c) The LinkedList will be replaced by another hashmap
d) Any further addition throws Overflow exception

Answer: a
Clarification: BalancedTree will improve performance from O(n) to O(log n) by reducing hash collisions.

Leave a Reply

Your email address will not be published. Required fields are marked *