Java Concurrency: Basics of Locks and Synchronizers
A beginner's quiz on understanding locks and synchronizers in Java concurrency.
Questions in this quiz
15 questions on Java Concurrency: Basics of Locks and Synchronizers. Work through them here, then start the quiz to answer them and see your score.
1.What is the primary purpose of using locks in Java concurrency?
- ATo increase the number of threads that can run simultaneously
- BTo ensure that only one thread can access a resource at a time
- CTo speed up the execution of threads
- DTo reduce memory usage
2.Which interface in Java provides a more flexible locking mechanism than synchronized blocks?
- ACallable
- BLock
- CRunnable
- DThread
3.What method must be called to release a lock obtained using the Lock interface?
- Aunlock()
- Bexit()
- Crelease()
- Dfree()
4.Which class is used in Java to implement a counting semaphore?
- ACyclicBarrier
- BExchanger
- CCountDownLatch
- DSemaphore
5.What is the main difference between a ReentrantLock and a synchronized block?
- AReentrantLock does not support reentrancy
- BReentrantLock is slower than synchronized
- CReentrantLock is only available in Java 11 and above
- DReentrantLock can be used across methods, synchronized cannot
6.Which method in the Lock interface can be used to try acquiring a lock without blocking?
- Alock()
- Bacquire()
- ClockInterruptibly()
- DtryLock()
7.What is the purpose of the CountDownLatch class in Java?
- ATo exchange data between two threads
- BTo allow one or more threads to wait until a set of operations being performed in other threads completes
- CTo synchronize access to a shared resource
- DTo manage a pool of threads
8.Which method in the Semaphore class is used to acquire a permit?
- Apermit()
- Bget()
- Clock()
- Dacquire()
9.What is a key feature of the ReentrantLock class?
- AIt supports multiple condition variables
- BIt cannot be interrupted
- CIt is not reentrant
- DIt does not support fairness
10.Which class in Java is used to synchronize the start of multiple threads?
- ASemaphore
- BCyclicBarrier
- CCountDownLatch
- DReentrantLock
11.What does the await() method in CountDownLatch do?
- AReleases a permit
- BDecreases the count by one
- CBlocks the current thread until the count reaches zero
- DIncreases the count by one
12.Which of the following is true about the synchronized keyword?
- AIt is faster than using a Lock object
- BIt can only be used on instance methods
- CIt can be used to synchronize static methods
- DIt does not support reentrancy
13.What is the primary use of the Exchanger class in Java?
- ATo synchronize access to a shared resource
- BTo exchange data between two threads
- CTo manage a pool of threads
- DTo count down a latch
14.Which method in the CyclicBarrier class is used to wait until all parties have invoked await?
- Ajoin()
- Bwait()
- Csynchronize()
- Dawait()
15.What happens if a thread calls lock() on a ReentrantLock it already holds?
- AIt releases the lock
- BIt blocks until the lock is released
- CIt throws an exception
- DIt increments the hold count and continues