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.22Medium
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.23Medium
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.24Medium
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.
Q.25Medium
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.
Advertisement
Q.26Medium
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.27Medium
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.28Medium
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.29Medium
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.30Medium
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.31Medium
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.32Medium
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.
Q.33Medium
Which of the following is true about CountDownLatch?
Answer: A
CountDownLatch is a one-time use synchronizer. Once the count reaches zero, it cannot be reset. CyclicBarrier is reusable.
Q.34Medium
What is the purpose of the wait() method in Java?
Answer: A
wait() releases the monitor lock and causes the thread to wait until notify() or notifyAll() is called by another thread, enabling inter-thread communication.
Q.35Medium
In a synchronized block, if notifyAll() is called, what happens?
Answer: A
notifyAll() wakes up all threads waiting on the monitor. They will then compete to acquire the lock when it's released.
Q.36Medium
What is the primary difference between Semaphore and Mutex?
Answer: A
A Semaphore maintains a count allowing multiple threads (based on permit count) to access a resource. A Mutex (binary semaphore with count=1) allows only one thread.
Q.37Medium
What happens when Thread.interrupt() is called on a thread?
Answer: A
interrupt() sets an internal interrupt flag. The thread must check this flag using isInterrupted() or interrupted() and handle it appropriately.
Q.38Medium
In a deadlock scenario, which of the following is always true?
Answer: A
Deadlock occurs when multiple threads are blocked indefinitely, each waiting for a resource held by another, creating a circular wait condition.
Q.39Medium
What is the key difference between Callable and Runnable?
Answer: A
Callable<V> can return a value of type V and throw checked exceptions. Runnable has void run() method and cannot throw checked exceptions.
Q.40Medium
What will happen if you try to call start() on a thread that has already completed execution?
Answer: B
Once a thread completes (reaches TERMINATED state), calling start() again throws IllegalThreadStateException. A thread can only be started once.