What will be the output of the following code?
Object lock = new Object();
synchronized(lock) {
synchronized(lock) {
System.out.println("Nested");
}
}
Answer: A
Java supports reentrant locks on synchronized blocks. The same thread can acquire the same lock multiple times. The lock is released only when the outermost synchronized block exits.
Q.342Medium
Which interface would you use to submit multiple tasks and wait for all of them to complete?
Answer: B
Callable interface returns a result via Future, and ExecutorService.invokeAll() waits for all submitted tasks to complete. This is the standard pattern for parallel task execution with result collection.
Q.343Medium
What is the difference between notify() and notifyAll() in Java?
Answer: A
notify() randomly selects one waiting thread to wake up, while notifyAll() wakes all waiting threads. notifyAll() is generally safer to avoid missed notifications when multiple threads are waiting on the same condition.
Q.344Medium
Which of the following best describes CyclicBarrier?
Answer: A
CyclicBarrier is a synchronizer that allows a fixed number of threads to wait for each other at a barrier point. Once all threads reach the barrier, they proceed together. It's reusable (cyclic) unlike CountDownLatch.
Q.345Medium
What happens when a thread acquires a lock on an object and then calls wait()?
Answer: B
When wait() is called, the thread releases the lock it holds on the object and enters the waiting pool. Another thread can then acquire the lock. The thread re-acquires the lock when notified.
Advertisement
Q.346Easy
Which collection class is thread-safe without explicit synchronization?
Answer: C
ConcurrentHashMap uses segment-based locking (in older versions) or fine-grained locking to provide thread-safety without synchronizing the entire map. Other options require external synchronization or Collections.synchronizedMap().
Q.347Medium
In a scenario where Thread A holds Lock1 and waits for Lock2, while Thread B holds Lock2 and waits for Lock1, what is this called?
Answer: B
This is a classic deadlock scenario involving circular wait. Race condition involves competing for resources. Starvation is when a thread never gets the resource. Livelock is when threads keep changing states without progress.
Q.348Medium
What is the output of this code?
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> System.out.println("Task 1"));
executor.submit(() -> System.out.println("Task 2"));
executor.shutdown();
Answer: B
newSingleThreadExecutor() creates an executor with exactly one thread. Tasks are queued and executed sequentially in the order submitted. Task 1 will always execute before Task 2.
Q.349Medium
Which method is used to forcefully stop a thread in modern Java?
Answer: D
Thread.stop() is deprecated and unsafe. Modern approaches use either a volatile flag or Thread.interrupt() with proper handling. Both are valid and recommended depending on the scenario.
Q.350Hard
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.351Hard
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.352Medium
What happens if an exception is thrown inside a synchronized block?
Answer: A
Java guarantees that the lock is released when exiting a synchronized block, whether normally or via an exception. This is why synchronized is considered safer than manual lock management.
Q.353Hard
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.354Hard
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.355Hard
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.356Easy
Which method is used to prevent race conditions by allowing only one thread to access a resource at a time?
Answer: A
The synchronized keyword creates a critical section that only one thread can access at a time, preventing race conditions. volatile ensures visibility but not atomicity.
Q.357Medium
In Java 21 Virtual Threads, what is the key advantage over platform threads?
Answer: A
Virtual threads (Project Loom) are extremely lightweight and allow creating millions of threads efficiently, unlike platform threads which are limited by OS resources.
Q.358Medium
What does the volatile keyword guarantee in Java multithreading?
Answer: A
volatile ensures that all threads see the most recent value of a variable (visibility) but does not provide atomicity for compound operations.
Q.359Medium
What is the output of the following code?
volatile int counter = 0;
counter++; // in multiple threads
Which issue may occur?
Answer: A
volatile only ensures visibility, not atomicity. counter++ is actually three operations (read, increment, write), so even with volatile, race conditions can occur.
Q.360Medium
What is CyclicBarrier used for in multithreading?
Answer: A
CyclicBarrier allows a set of threads to wait for each other to reach a common point, then all proceed together. It's reusable unlike CountDownLatch.