Java ConcurrentHashMap & Thread Safety: Intermediate Concepts
Assess your understanding of Java's ConcurrentHashMap and thread safety mechanisms.
Questions in this quiz
12 questions on Java ConcurrentHashMap & Thread Safety: Intermediate Concepts. Work through them here, then start the quiz to answer them and see your score.
1.What is the primary advantage of using ConcurrentHashMap over HashMap in a multi-threaded environment?
- AIt provides thread-safe operations without locking the entire map.
- BIt allows null keys and values.
- CIt automatically sorts the entries.
- DIt uses less memory.
2.Which method in ConcurrentHashMap ensures atomic updates?
- Asize()
- BputIfAbsent()
- Cclear()
- DcontainsKey()
3.How does ConcurrentHashMap achieve thread safety?
- ABy partitioning the map into segments, each with its own lock.
- BBy using a single lock for the entire map.
- CBy using synchronized blocks for each operation.
- DBy using immutable data structures.
4.Which of the following operations is NOT atomic in ConcurrentHashMap?
- Aremove()
- Bget()
- Cput()
- Dcompute()
5.What happens if you try to store a null key or value in a ConcurrentHashMap?
- AIt replaces the null with a default value.
- BIt ignores the null entry.
- CIt throws a NullPointerException.
- DIt stores the null value without any issue.
6.Which method would you use to atomically compute a new value for a key in ConcurrentHashMap?
- Amerge()
- BcontainsValue()
- Creplace()
- Dput()
7.What is the default concurrency level in ConcurrentHashMap?
- A16
- B32
- C8
- D1
8.Which method in ConcurrentHashMap is used to perform a given action for each entry?
- Astream()
- BkeySet()
- CentrySet()
- DforEach()
9.Why is ConcurrentHashMap preferred over Collections.synchronizedMap()?
- AIt supports null keys and values.
- BIt uses less memory.
- CIt is faster due to less locking.
- DIt is easier to implement.
10.Which method is used to remove a mapping only if it is currently mapped to a specified value?
- Aremove()
- Breplace()
- CremoveIf()
- DcomputeIfAbsent()
11.What is the effect of the computeIfAbsent() method in ConcurrentHashMap?
- AIt computes a value only if the key is already present.
- BIt removes the entry if the computed value is null.
- CIt computes a value only if the key is not already associated with a value.
- DIt computes a value and replaces the existing one.
12.Which of the following is a key benefit of using ConcurrentHashMap in Java?
- AIt automatically balances load across threads.
- BIt provides built-in sorting of keys.
- CIt guarantees iteration order.
- DIt allows for concurrent reads and writes without blocking.