This set of Java Multiple Choice Questions & Answers (MCQs) on “Data Structures-HashMap”.
1. Map implements collection interface? Answer: b 2. Which of the below does not implement Map interface? Answer: d 3. What is the premise of equality for IdentityHashMap? Answer: a 4. What happens if we put a key object in a HashMap which exists? Answer: a 5. While finding the correct location for saving key value pair, how many times the key is hashed? Answer: b 6. Is hashmap an ordered collection. Answer: b 7. If two threads access the same hashmap at the same time, what would happen? Answer: a 8. How to externally synchronize hashmap? c) Collections.synchronizedMap(new HashMap Answer: c 9. What will be the output of the following Java code snippet? a) {1=null, 2=null, 3=null, 4=null, 5=null} Answer: a 10. If large number of items are stored in hash bucket, what happens to the internal structure? Answer: a
a) True
b) False
Clarification: Collection interface provides add, remove, search or iterate while map has clear, get, put, remove, etc.
a) HashMap
b) Hashtable
c) EnumMap
d) Vector
Clarification: Vector implements AbstractList which internally implements Collection. Others come from implementing the Map interface.
a) Reference equality
b) Name equality
c) Hashcode equality
d) Length equality
Clarification: IdentityHashMap is rarely used as it violates the basic contract of implementing equals() and hashcode() method.
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
Clarification: HashMap always contains unique keys. If same key is inserted again, the new object replaces the previous object.
a) 1
b) 2
c) 3
d) unlimited till bucket is found
Clarification: The key is hashed twice; first by hashCode() of Object class and then by internal hashing method of HashMap class.
a) True
b) False
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.
a) ConcurrentModificationException
b) NullPointerException
c) ClassNotFoundException
d) RuntimeException
Clarification: The code will throw ConcurrentModificationException if two threads access the same hashmap at the same time.
a) HashMap.synchronize(HashMap a);
b) HashMap a = new HashMap();
a.synchronize();
d) Collections.synchronize(new HashMap
Clarification: Collections.synchronizedMap() synchronizes entire map. ConcurrentHashMap provides thread safety without synchronizing entire map.
public class Demo
{
public static void main(String[] args)
{
Map<Integer, Object> sampleMap = new TreeMap<Integer, Object>();
sampleMap.put(1, null);
sampleMap.put(5, null);
sampleMap.put(3, null);
sampleMap.put(2, null);
sampleMap.put(4, null);
System.out.println(sampleMap);
}
}
b) {5=null}
c) Exception is thrown
d) {1=null, 5=null, 3=null, 2=null, 4=null}
Clarification: HashMap needs unique keys. TreeMap sorts the keys while storing objects.
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
Clarification: BalancedTree will improve performance from O(n) to O(log n) by reducing hash collisions.