Java ConcurrentHashMap MCQ Questions and Answers
Q. What is the key difference between HashMap and ConcurrentHashMap in Java?
A. HashMap is thread-safe, while ConcurrentHashMap is not.B. ConcurrentHashMap allows multiple threads to access and modify it concurrently, while HashMap is not designed for concurrent access.
C. There is no difference; they are just different names for the same class.
D. HashMap is faster than ConcurrentHashMap for single-threaded applications.
Q. Which of the following is true about the concurrency level in ConcurrentHashMap?
A. The concurrency level determines the number of buckets in the table.B. It represents the maximum number of threads that can access the map concurrently.
C. It is the initial number of segments the map is divided into.
D. The concurrency level has no impact on performance.
Q. How does ConcurrentHashMap handle collisions?
A. It uses separate chaining with linked lists.B. It uses open addressing with linear probing.
C. It uses a balanced binary search tree for collision resolution.
D. It uses a hash table with a load factor of 0.75 to minimize collisions.
Q. Which of the following methods in ConcurrentHashMap is NOT atomic?
A. putB. get
C. remove
D. size
Q. What happens when the load factor of a ConcurrentHashMap exceeds its threshold?
A. It throws a RuntimeException.B. It automatically increases the concurrency level.
C. It doubles the number of buckets in the table.
D. It rehashes the table and redistributes the entries.
Q. How can you iterate over the keys in a ConcurrentHashMap?
A. Using a for-each loop with the keySet() methodB. By obtaining an Iterator from the keySet() method
C. Using the keys() method to get an array of keys
D. All of the above
Q. What is the purpose of the putIfAbsent method in ConcurrentHashMap?
A. It puts a value in the map only if the key is present.B. It puts a value in the map only if the key is absent.
C. It returns the previous value associated with the key.
D. It returns true if the key was already present in the map.