Mutex vs Semaphore — Concept Check
Questions on Mutex vs Semaphore. A mutex is a binary lock with ownership used for mutual exclusion; a semaphore is an integer counter without ownership used for signalling and resource counting.
Questions in this quiz
7 questions on Mutex vs Semaphore — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer uses a mutex to protect a critical section in a multithreaded application. However, the application occasionally deadlocks. What could be a potential cause?
- AThe mutex is not reentrant, causing repeated locks to deadlock.
- BThe mutex is not being unlocked by the owning thread.
- CThe mutex is being used as a semaphore, causing resource contention.
- DThe mutex is being locked by multiple threads simultaneously.
2.In a Java application, a developer uses a semaphore to manage access to a pool of database connections. Occasionally, the application runs out of connections. What might be happening?
- AThe semaphore is not releasing permits properly.
- BThe semaphore is not initialized, causing resource leaks.
- CThe semaphore's permit count is too low, limiting access.
- DThe semaphore is being used as a mutex, causing deadlocks.
3.A developer mistakenly thinks that a binary semaphore and a mutex are interchangeable. Which of the following is a key difference?
- AA mutex can be used for resource counting.
- BA mutex has ownership; a binary semaphore does not.
- CA binary semaphore can only be used for signaling.
- DA binary semaphore is always fair in thread scheduling.
4.In a multithreaded Java application, a developer uses ReentrantLock. What advantage does ReentrantLock provide over synchronized blocks?
- AReentrantLock allows reentrancy, preventing deadlocks.
- BReentrantLock supports tryLock and timed lock acquisition.
- CReentrantLock automatically releases locks on exceptions.
- DReentrantLock is faster than synchronized blocks.
5.A developer uses a semaphore with permits initialized to 3 to control access to a resource. What happens if four threads try to acquire the semaphore simultaneously?
- AThe semaphore automatically increases its permit count.
- BThe semaphore throws an exception due to insufficient permits.
- CAll threads acquire the semaphore without blocking.
- DThe fourth thread is blocked until a permit is released.
6.Which of the following is true about Java's ReentrantLock?
- AIt allows the same thread to lock multiple times without deadlocking.
- BIt is always fair in thread scheduling.
- CIt does not support condition variables.
- DIt automatically unlocks when a thread terminates.
7.What is the primary purpose of a mutex in concurrent programming?
- ATo signal between producer and consumer threads.
- BTo manage access to a pool of identical resources.
- CTo ensure mutual exclusion in critical sections.
- DTo count resources in a multithreaded environment.