In the context of synchronized methods, what is acquired and released automatically?
Answer: B
Synchronized methods automatically acquire and release the monitor lock (intrinsic lock) associated with the object, ensuring thread safety.
Q.162Medium
What is the main advantage of using ExecutorService over directly creating threads?
Answer: B
ExecutorService manages a pool of threads, reusing them for multiple tasks, which reduces overhead and improves resource utilization.
Q.163Medium
In a multithreaded application, what does the term 'context switching' refer to?
Answer: C
Context switching is the OS mechanism of switching CPU execution between different threads, saving and restoring thread state.
Q.164Medium
In Java 21 (latest), which is a modern approach to create thread-safe operations?
Answer: B
Java 21 introduced Virtual Threads as a lightweight threading model under Project Loom, allowing millions of concurrent tasks. This is more efficient than platform threads for high-concurrency scenarios.
Q.165Medium
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.
Advertisement
Q.166Medium
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.167Medium
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.168Medium
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.169Medium
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.170Medium
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.171Medium
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.172Medium
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.173Medium
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.174Medium
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.175Medium
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.176Medium
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.177Medium
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.178Medium
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.179Medium
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.180Medium
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.