What happens when wait() is called from a thread that doesn't hold the lock?
Answer: B
wait() must be called from within a synchronized context. Calling it without holding the lock throws IllegalMonitorStateException.
Q.2Hard
How does the volatile keyword help in multithreading?
Answer: B
The volatile keyword ensures that changes to a variable are immediately visible to all threads, providing memory visibility without the overhead of full synchronization.
Q.3Hard
What will happen if you call sleep() inside a synchronized block?
Answer: B
When sleep() is called inside a synchronized block, the thread retains the lock on the object, preventing other threads from entering the synchronized section.
Q.4Hard
Which interface allows multiple threads to access a resource with a permit system?
Answer: B
Semaphore is a synchronization utility that uses a counter to control access to a shared resource. Threads can acquire and release permits.
Q.5Hard
Consider code where Thread A holds Lock1 and waits for Lock2, while Thread B holds Lock2 and waits for Lock1. What is this scenario called?
Answer: C
This is a classic circular wait scenario resulting in deadlock, where neither thread can proceed because each is waiting for a resource held by the other.
Advertisement
Q.6Hard
Consider the following code. What will be the likely behavior?
java
synchronized void method1() { method2(); }
synchronized void method2() { }
Answer: C
Java's intrinsic locks are reentrant, meaning the same thread can acquire the same lock multiple times. The thread can call method2() from method1() without deadlock.
Q.7Hard
Which class is used to synchronize the execution of multiple threads at a specific point?
Answer: B
CyclicBarrier allows multiple threads to wait for each other at a specific point. CountDownLatch is for one-time synchronization; CyclicBarrier is reusable.
Q.8Hard
What is the difference between CountDownLatch and CyclicBarrier?
Answer: B
CountDownLatch is used for one-time synchronization where threads wait for a countdown to reach zero. CyclicBarrier can be reused after threads are released.
Q.9Hard
What will happen if a thread tries to acquire a lock it already holds while inside a synchronized method?
Answer: C
Java's monitors are reentrant. A thread can acquire the same lock multiple times. An internal counter tracks lock acquisitions and releases.
Q.10Hard
In a high-concurrency scenario with 2024-25 exam patterns, which Java construct provides the best lock-free approach for thread safety?
Answer: C
Atomic variables use Compare-And-Swap (CAS) operations for lock-free thread safety, providing better performance in high-concurrency scenarios compared to traditional locking.
Q.11Hard
What is the primary advantage of using ReentrantLock over synchronized?
Answer: B
ReentrantLock provides more control with tryLock() for non-blocking attempts, Condition objects for complex wait/notify patterns, and fairness policy. It's more flexible than synchronized.
Q.12Hard
In a high-traffic web application using Java 21 Virtual Threads, what is the main performance benefit?
Answer: B
Virtual Threads are lightweight (millions can run) with minimal memory overhead compared to platform threads (thousands). They automatically handle I/O blocking without thread creation, making them ideal for high-concurrency scenarios.
Q.13Hard
Which pattern should be used to safely publish data from one thread to another?
Answer: B
Safe publication requires using visibility mechanisms: synchronized (mutual exclusion), volatile (visibility), or thread-safe collections (both). Direct assignment without synchronization causes visibility issues in the memory model.
Q.14Hard
What is the purpose of ForkJoinPool in Java?
Answer: B
ForkJoinPool is designed for divide-and-conquer tasks where large problems are split (fork) into smaller subtasks and results are combined (join). It's optimized for recursive parallel algorithms.
Q.15Hard
If a thread is blocked waiting for I/O, what happens when interrupt() is called on it?
Answer: B
Calling interrupt() on a blocked thread sets the interrupt flag. If the blocking operation supports interruption (like sleep(), join(), wait()), it throws InterruptedException. Non-interruptible I/O blocks require other mechanisms.
Q.16Hard
Consider a scenario: Thread A is waiting in wait() inside a synchronized block. Thread B calls notify(). What is the state of Thread A?
Answer: A
After notify(), the waiting thread transitions to runnable state, but it must still acquire the lock before resuming execution within the synchronized block.
Q.17Hard
In a high-concurrency scenario using Java 21, which approach is recommended for I/O-bound operations?
Answer: A
Virtual Threads (Project Loom) are ideal for I/O-bound operations as they have minimal overhead and can number in millions, improving scalability significantly.
Q.18Hard
What is the purpose of the strictfp modifier in the context of multithreading?
Answer: A
strictfp ensures consistent floating-point results across different platforms/JVMs, though it's not directly a multithreading construct.
Q.19Hard
In a producer-consumer problem, what is the ideal synchronization mechanism?
Answer: A
BlockingQueue (like LinkedBlockingQueue) is purpose-built for producer-consumer patterns, handling synchronization and blocking elegantly.
Q.20Hard
Consider a scenario with 3 threads updating a shared counter. Which synchronization mechanism is MOST efficient?
Answer: B
AtomicInteger uses lock-free CAS operations which are more efficient than synchronized blocks or locks for simple counter operations with multiple threads.