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
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
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
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
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
Explanation: 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
View Answer
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
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);
b)
HashMap a = new HashMap(); a.synchronize();
c) Collections.synchronizedMap(new HashMap<String, String>());
d) Collections.synchronize(new HashMap<String, String>());
View Answer
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?
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);
}
}
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
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
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.
- Practice Programming MCQs
- Practice BCA MCQs
- Practice Information Technology MCQs
- Check Java Books
- Check Programming Books