Monitors & Condition Variables — Concept Check
Questions on Monitors & Condition Variables. A monitor combines a mutex and condition variables into a single high-level construct; threads call wait() to release the lock and sleep, and notify() to wake sleeping threads.
Questions in this quiz
8 questions on Monitors & Condition Variables — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.Two threads are using a Java object as a monitor. One thread calls wait() and another calls notify(). What must happen for the waiting thread to proceed?
- AThe notifying thread must release the lock before notify() is called.
- BThe waiting thread does not need to re-acquire the lock.
- CThe waiting thread proceeds immediately after notify() is called.
- DThe waiting thread must re-acquire the lock after being notified.
2.In a Java program, a thread calls wait() inside a synchronized block. What happens if notify() is never called?
- AThe thread will eventually be woken due to spurious wakeups.
- BThe thread will automatically retry after a fixed interval.
- CThe thread remains blocked indefinitely.
- DThe thread will throw an exception after a timeout.
3.A developer uses ReentrantLock with multiple Condition objects. Why might this be preferred over a single intrinsic monitor?
- AIt automatically handles all conditions without explicit signaling.
- BIt reduces spurious wakeups by targeting specific conditions.
- CIt provides automatic deadlock prevention.
- DIt simplifies the code by using fewer synchronization constructs.
4.A thread in Java calls wait() but does not check the condition in a while loop. What issue might arise?
- AThe thread may proceed on a spurious wakeup without the condition being true.
- BThe thread will never proceed as it will be stuck in wait().
- CThe thread will throw an IllegalMonitorStateException.
- DThe thread will proceed correctly as long as notify() is called.
5.In a producer-consumer scenario using ReentrantLock, why is notEmpty.signal() used instead of notifyAll()?
- AIt prevents deadlocks by signaling all waiting threads.
- BIt wakes all threads, ensuring no thread is left waiting.
- CIt wakes only consumers, improving throughput.
- DIt automatically handles synchronization without explicit locking.
6.A Java object is used as a monitor with synchronized methods. What happens if a thread calls notifyAll() while no threads are waiting?
- AThe calling thread is blocked until another thread waits.
- BNo effect; notifyAll() does nothing if no threads are waiting.
- CAll threads are woken regardless of their wait status.
- DAn exception is thrown for calling notifyAll() unnecessarily.
7.In Java, why should the wait condition be checked in a while loop instead of an if statement?
- ATo allow the thread to wait for multiple conditions simultaneously.
- BTo handle spurious wakeups that can occur unpredictably.
- CTo prevent IllegalMonitorStateException from being thrown.
- DTo ensure the thread never enters the wait state unnecessarily.
8.Which of the following correctly describes a monitor in Java?
- AA tool for prioritizing thread execution based on conditions.
- BA synchronization construct combining a mutex and condition variables.
- CA feature that eliminates the need for explicit locking.
- DA mechanism for automatic deadlock prevention.