Java Questions & Answers – Data Structures-HashMap

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

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

Answer: b
Explanation: 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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: b
Explanation: The key is hashed twice; first by hashCode() of Object class and then by internal hashing method of HashMap class.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

Answer: b
Explanation: 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
View Answer

Answer: a
Explanation: 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);

HashMap a = new HashMap();

c) Collections.synchronizedMap(new HashMap<String, String>());
d) Collections.synchronize(new HashMap<String, String>());
View Answer

Answer: c
Explanation: 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); 
  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}
View Answer

Answer: a
Explanation: 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
View Answer

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

Sanfoundry Global Education & Learning Series – Java Programming Language.

To practice all areas of Java language, here is complete set of 1000+ Multiple Choice Questions and Answers.

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.