Which scenario can lead to livelock in multithreading?
ATwo threads continuously change state in response to each other but never make progress
BOne thread blocks another thread indefinitely
CMultiple threads access the same resource without synchronization
DA thread is waiting for a resource held by another waiting thread
Correct Answer:
A. Two threads continuously change state in response to each other but never make progress
EXPLANATION
Livelock occurs when threads are not blocked but continuously change state in response to each other (like two people trying to pass each other), preventing progress. This differs from deadlock where threads are blocked.
What is the behavior of ReentrantReadWriteLock when multiple threads perform read operations?
AOnly one thread can read at a time
BMultiple threads can read simultaneously, but writing locks them out
CThreads must alternate between read and write operations
DAll threads must wait for exclusive lock
Correct Answer:
B. Multiple threads can read simultaneously, but writing locks them out
EXPLANATION
ReentrantReadWriteLock allows multiple threads to acquire the read lock simultaneously, but only one thread can hold the write lock. This improves concurrency for read-heavy workloads.
Consider a scenario with 3 threads updating a shared counter. Which synchronization mechanism is MOST efficient?
Asynchronized block on counter
BAtomicInteger
CReentrantLock
DSemaphore with permits=1
Correct Answer:
B. AtomicInteger
EXPLANATION
AtomicInteger uses lock-free CAS operations which are more efficient than synchronized blocks or locks for simple counter operations with multiple threads.
What exception does CyclicBarrier throw when a thread is interrupted while waiting?
AInterruptedException
BBrokenBarrierException
CCyclicBarrierException
DThreadInterruptedException
Correct Answer:
A. InterruptedException
EXPLANATION
When a thread waiting at a CyclicBarrier is interrupted, it throws InterruptedException. This breaks the barrier and causes other waiting threads to receive BrokenBarrierException.
What is the output of this code snippet?
ExecutorService es = Executors.newFixedThreadPool(2);
es.execute(() -> System.out.println("Task 1"));
es.shutdown();
ATask 1 will be executed before the program terminates
BTask 1 may or may not be executed
CCompilation error
DTask 1 will never execute
Correct Answer:
A. Task 1 will be executed before the program terminates
EXPLANATION
shutdown() gracefully shuts down the executor service by rejecting new tasks but allowing submitted tasks to complete. Task 1 is already submitted before shutdown(), so it will execute.
In Java 21, which new feature was introduced for concurrent programming?
AVirtual Threads (Project Loom)
BReentrantLock
CCountDownLatch
DPhaser
Correct Answer:
A. Virtual Threads (Project Loom)
EXPLANATION
Java 21 introduced Virtual Threads as part of Project Loom, which are lightweight threads that make it easier to write scalable concurrent applications.
What does the volatile keyword guarantee in multithreading?
APrevents thread access completely
BVisibility of changes across threads and prevents instruction reordering
CMakes operations atomic
DAutomatically synchronizes all methods
Correct Answer:
B. Visibility of changes across threads and prevents instruction reordering
EXPLANATION
volatile ensures that changes to a variable are immediately visible to all threads and prevents the JVM from reordering instructions. However, it doesn't make operations atomic.
Which Java class provides thread-safe operations using Compare-And-Swap (CAS)?
AVector
BCollections.synchronizedList()
CAtomicInteger
DConcurrentHashMap
Correct Answer:
C. AtomicInteger
EXPLANATION
AtomicInteger and other Atomic* classes use CAS operations for lock-free thread-safe operations. ConcurrentHashMap uses segment-based locking, not CAS.
What is the purpose of the yield() method in Java threading?
ATo permanently stop the current thread
BTo suggest that the current thread should yield CPU to other threads of equal priority
CTo make a thread wait indefinitely
DTo increase thread priority
Correct Answer:
B. To suggest that the current thread should yield CPU to other threads of equal priority
EXPLANATION
yield() is a hint to the thread scheduler that the current thread is willing to yield its current use of CPU. It doesn't guarantee the thread will yield, as scheduling is JVM-dependent.
Consider the code:
synchronized void method1() { wait(); }
If wait() is called without a lock, what happens?
AThe thread waits indefinitely
BIllegalMonitorStateException is thrown
CThe method returns normally
DCompilation error occurs
Correct Answer:
B. IllegalMonitorStateException is thrown
EXPLANATION
wait() must be called from within a synchronized block or method. If called outside synchronized context, it throws IllegalMonitorStateException at runtime.