Search Tutorials


Java ConcurrentHashMap MCQ Questions and Answers | JavaInUse

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. put
B. 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() method
B. 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.





Q. What is the default initial capacity of a ConcurrentHashMap?

A. 0
B. 16
C. 32
D. It is dynamically calculated based on the expected number of elements.

Q. What is the result of calling the clear() method on a ConcurrentHashMap?

A. It removes all entries from the map.
B. It sets the size of the map to zero, but keeps the entries.
C. It throws an UnsupportedOperationException.
D. It returns a new ConcurrentHashMap with no entries.

Q. How does ConcurrentHashMap handle null keys and values?

A. It allows null keys and values.
B. It throws a NullPointerException for null keys and values.
C. It treats null keys and values as equivalent to empty strings.
D. It allows null keys but not null values.

Q. What is the purpose of the computeIfAbsent method in ConcurrentHashMap?

A. It computes and returns the value associated with a key, creating it if absent.
B. It returns the value associated with a key, or null if the key is absent.
C. It associates the given value with the key, overwriting any existing value.
D. It removes the entry for the specified key if it is present.

Q. What is the expected output of this code snippet? ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>(); map.put("one", 1); map.put("two", 2); map.put("three", 3); System.out.println(map.get("two"));

A. null
B. 1
C. 2
D. It will throw an exception.

Q. How can you create a ConcurrentHashMap that uses access ordering instead of insertion ordering?

A. By setting the accessOrder parameter to true in the constructor
B. By calling the orderByKey() method on the map
C. By using the LinkedHashMap class instead
D. ConcurrentHashMap does not support access ordering.

Q. What is the purpose of the forEach method in ConcurrentHashMap?

A. It applies a function to each key-value pair in the map.
B. It iterates over the keys in the map and performs an action.
C. It returns a new map containing the results of applying a function to each key-value pair.
D. It is used to iterate over the values in the map.

Q. What is the result of calling the containsValue method with a null value on a ConcurrentHashMap that contains null values?

A. true
B. false
C. It throws a NullPointerException.
D. It depends on the implementation.