Advanced Java: Mastering ConcurrentHashMap
An in-depth quiz on the intricacies of Java's ConcurrentHashMap for thread-safe collections.
Questions in this quiz
10 questions on Advanced Java: Mastering ConcurrentHashMap. 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 Hashtable in Java?
- AIt is faster because it uses a single lock for the entire map.
- BIt is immutable and thus thread-safe.
- CIt allows multiple threads to read and write concurrently.
- DIt supports null keys and values.
2.How does ConcurrentHashMap achieve thread-safety without locking the entire map?
- ABy partitioning the map into segments, each with its own lock.
- BBy using a global lock.
- CBy using synchronized blocks for each operation.
- DBy using immutable data structures.
3.Which method in ConcurrentHashMap is atomic and can be used to safely update a value?
- Acompute()
- Bmerge()
- Creplace()
- DputIfAbsent()
4.What happens if a null key is used in a ConcurrentHashMap?
- AThe null key is ignored.
- BThe map automatically assigns a default key.
- CThe null key is accepted and stored.
- DA NullPointerException is thrown.
5.Which of the following operations are not supported by ConcurrentHashMap?
- AAtomic updates
- BIteration over keys
- CConcurrent reads
- DNull values
6.Why is ConcurrentHashMap preferred over synchronizedMap for high-concurrency scenarios?
- AIt uses less memory.
- BIt provides better performance by reducing contention.
- CIt is easier to implement.
- DIt supports more data types.
7.What is the default concurrency level in ConcurrentHashMap?
- A8
- B32
- C1
- D16
8.Which method would you use to compute a new mapping for a key only if it is already present?
- AcomputeIfPresent()
- Breplace()
- Cput()
- DcomputeIfAbsent()
9.How does ConcurrentHashMap handle failed updates due to concurrent modifications?
- AIt throws a ConcurrentModificationException.
- BIt retries the update automatically.
- CIt uses a compare-and-swap mechanism.
- DIt logs the failure and continues.
10.Which of the following is a characteristic of the compute() method in ConcurrentHashMap?
- AIt is deprecated in Java 8.
- BIt allows null keys.
- CIt locks the entire map during computation.
- DIt atomically computes a new value for a given key.