Advanced Java Concurrency: Mastering Locks and Synchronization
An advanced quiz on the nuances of locks and synchronization in Java concurrency.
Questions in this quiz
10 questions on Advanced Java Concurrency: Mastering Locks and Synchronization. Work through them here, then start the quiz to answer them and see your score.
1.In Java, what is the primary difference between using a synchronized block and a ReentrantLock?
- AReentrantLock is part of the Java language, while synchronized is part of the Java API.
- BSynchronized blocks are more flexible than ReentrantLock.
- CSynchronized blocks can be used in more complex scenarios than ReentrantLock.
- DReentrantLock provides more control over lock acquisition and release.
2.Which of the following is a feature of ReentrantLock that synchronized blocks do not offer?
- AAutomatic unlocking on exception
- BAtomicity
- CThread safety
- DFairness policy
3.How does the performance of synchronized blocks compare to ReentrantLock in high-contention scenarios?
- ABoth have the same performance.
- BSynchronized blocks are generally faster.
- CPerformance depends on the JVM version.
- DReentrantLock is generally faster.
4.What happens if a thread holding a ReentrantLock does not release it?
- AIt causes a deadlock.
- BOther threads can acquire the lock after a timeout.
- CThe lock is released when the thread terminates.
- DThe JVM automatically releases it.
5.Which method would you use to attempt to acquire a ReentrantLock without blocking?
- Alock()
- Bunlock()
- CtryLock()
- DlockInterruptibly()
6.What is a potential downside of using ReentrantLock over synchronized blocks?
- AReentrantLock cannot be used in a try-finally block.
- BReentrantLock is not thread-safe.
- CReentrantLock requires explicit unlocking.
- DReentrantLock does not support reentrancy.
7.Which statement is true about the synchronized keyword in Java?
- AIt can synchronize on any object.
- BIt can only be used on methods.
- CIt is deprecated in Java 11.
- DIt provides fairness guarantees.
8.In what scenario would you prefer using synchronized over ReentrantLock?
- AFor simple synchronization needs.
- BWhen you need to handle interrupts.
- CWhen you need a fairness policy.
- DWhen you need to try acquiring a lock without blocking.
9.Which of the following is NOT a method provided by ReentrantLock?
- Asynchronized()
- BlockInterruptibly()
- CtryLock(long timeout, TimeUnit unit)
- Dlock()
10.What is the purpose of the lockInterruptibly() method in ReentrantLock?
- ATo acquire a lock and respond to interrupts.
- BTo acquire a lock with a fairness policy.
- CTo acquire a lock without blocking.
- DTo release a lock when interrupted.