Here are 10 essential multiple-choice questions on Java Concurrency Locks and Reentrant Locks, covering key concepts.
Question 1
What is the key difference between a Lock and a Monitor in Java concurrency?
Lock provides more flexibility for controlling concurrency than Monitor, which is associated with the synchronized keyword
Monitor allows multiple threads to acquire the same lock, while Lock allows only one thread to acquire a lock
Lock cannot be used for synchronization, but Monitor is used for it
Lock and Monitor are identical in usage and provide the same functionality
Question 2
Which of the following can help in deadlock prevention when acquiring multiple locks in a multithreaded program?
Acquiring locks in a consistent order
Using synchronized blocks for every lock
Using a Thread.sleep() method to delay threads
Limiting the number of locks used by the threads
Question 3
What does the ReentrantLock class provide that a synchronized block does not?
The ability to restrict a thread from acquiring the lock
The ability to handle deadlock situations automatically
The ability to acquire a lock multiple times by the same thread
The ability to synchronize only one block of code at a time
Question 4
Consider the following code snippet. Which of the following will result in a deadlock?
class A {
Lock lock1 = new ReentrantLock();
Lock lock2 = new ReentrantLock();
void method1() {
lock1.lock();
lock2.lock();
try {
// Critical section
} finally {
lock1.unlock();
lock2.unlock();
}
}
}
class B {
Lock lock1 = new ReentrantLock();
Lock lock2 = new ReentrantLock();
void method1() {
lock2.lock();
lock1.lock();
try {
// Critical section
} finally {
lock1.unlock();
lock2.unlock();
}
}
}
No deadlock will occur
Deadlock will occur if A.method1() and B.method1() are executed simultaneously
ReentrantLock always prevents deadlock
The code will fail due to incorrect usage of ReentrantLock
Question 5
Which of the following is a method provided by ReentrantLock to acquire a lock but allows the thread to be interrupted?
lock()
tryLock()
lockInterruptibly()
unlock()
Question 6
How can a thread avoid deadlock when acquiring multiple locks?
By acquiring all locks in the same order consistently
By always using synchronized blocks instead of ReentrantLock
By introducing random delays between lock acquisition
By acquiring a lock only if it is not already held by another thread
Question 7
What happens if two threads try to acquire the same lock using ReentrantLock simultaneously?
Both threads are allowed to acquire the lock at the same time
One thread will acquire the lock, and the other will block until the lock is released
Both threads will immediately terminate
The lock will be automatically split between the two threads
Question 8
Which of the following methods in ReentrantLock allows a thread to acquire the lock only if it is available, without waiting indefinitely?
lock()
tryLock()
lockInterruptibly()
unlock()
Question 9
Which of the following accurately describes a fairness policy in ReentrantLock?
It allows multiple threads to hold the lock simultaneously
It grants access to the longest-waiting thread
It prevents threads from acquiring the lock more than once
It disables lock acquisition temporarily
Question 10
What is the primary purpose of using ReentrantLock over the synchronized block in Java?
To ensure that only one thread can acquire a lock at any time
To provide more advanced features, such as timed lock acquisition and lock interruptibility
To guarantee the prevention of deadlocks
To allow multiple threads to acquire the lock concurrently
There are 10 questions to complete.