Java ConcurrentHashMap MCQ - Multiple Choice Questions and Answers

1. What is ConcurrentHashMap in Java?

a) A synchronized version of HashMap
b) A type of linked list
c) A type of array
d) A collection of sorted key-value pairs

2. How does ConcurrentHashMap achieve thread-safety?

a) By locking the entire map
b) By using synchronized methods
c) By segmenting the map into parts and locking those parts
d) By using immutable objects

3. Can ConcurrentHashMap contain null values or null keys?

a) Yes
b) No
c) Only null values
d) Only null keys

4. What is the default concurrency level of a ConcurrentHashMap?

a) 1
b) 4
c) 16
d) 32

5. Which method is used to replace a value in ConcurrentHashMap?

a) replace()
b) put()
c) set()
d) update()

6. What happens if two threads update a ConcurrentHashMap simultaneously?

a) The map gets corrupted
b) The map handles updates in a thread-safe manner
c) An exception is thrown
d) Only the first update is saved

7. Can you use an iterator to traverse a ConcurrentHashMap?

a) Yes
b) No
c) Only with synchronized blocks
d) Only in single-threaded environments

8. How does ConcurrentHashMap differ from Hashtable?

a) ConcurrentHashMap is not thread-safe
b) ConcurrentHashMap allows one null key and multiple null values
c) ConcurrentHashMap provides better scalability
d) ConcurrentHashMap does not use hashing

9. Is the iteration over ConcurrentHashMap fail-safe?

a) Yes
b) No
c) Only in single-threaded environments
d) Only if the map is not updated during iteration

10. How do you check if a ConcurrentHashMap is empty?

a) isEmpty()
b) isNull()
c) size() == 0
d) hasNoElements()

Comments