In Java 21 Virtual Threads, what is the key advantage over platform threads?
AVirtual threads are lighter weight and millions can be created without OS limitations
BVirtual threads are faster than platform threads
CVirtual threads do not require synchronization
DVirtual threads cannot cause deadlocks
Correct Answer:
A. Virtual threads are lighter weight and millions can be created without OS limitations
EXPLANATION
Virtual threads (Project Loom) are extremely lightweight and allow creating millions of threads efficiently, unlike platform threads which are limited by OS resources.
Which method is used to prevent race conditions by allowing only one thread to access a resource at a time?
Asynchronized
Bvolatile
Catomic
Dtransient
Correct Answer:
A. synchronized
EXPLANATION
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.
Correct Answer:
B. InterruptedException is thrown if the I/O operation supports interruption
EXPLANATION
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.
BTo divide work recursively using divide-and-conquer approach
CTo prevent thread creation
DTo serialize data across threads
Correct Answer:
B. To divide work recursively using divide-and-conquer approach
EXPLANATION
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.
Which pattern should be used to safely publish data from one thread to another?
ADirectly assign to a public static variable
BUse synchronized, volatile, or thread-safe collections like ConcurrentHashMap
CUse Thread.sleep() to ensure visibility
DNo special mechanism is needed in Java
Correct Answer:
B. Use synchronized, volatile, or thread-safe collections like ConcurrentHashMap
EXPLANATION
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.
What happens if an exception is thrown inside a synchronized block?
AThe lock is automatically released when exiting the block
BThe lock is permanently held
CThe exception propagates, but lock is not released
DThe thread terminates immediately
Correct Answer:
A. The lock is automatically released when exiting the block
EXPLANATION
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.
In a high-traffic web application using Java 21 Virtual Threads, what is the main performance benefit?
AVirtual Threads eliminate the need for synchronization
BVirtual Threads reduce memory overhead per thread, allowing millions of concurrent connections
CVirtual Threads guarantee no deadlocks
DVirtual Threads make single-threaded code faster
Correct Answer:
B. Virtual Threads reduce memory overhead per thread, allowing millions of concurrent connections
EXPLANATION
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.
What is the primary advantage of using ReentrantLock over synchronized?
AReentrantLock is always faster
BReentrantLock allows conditional waiting with Condition objects and tryLock()
CReentrantLock prevents all deadlocks automatically
DReentrantLock works with primitives
Correct Answer:
B. ReentrantLock allows conditional waiting with Condition objects and tryLock()
EXPLANATION
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.
Which method is used to forcefully stop a thread in modern Java?
AThread.stop() method
BUsing a shared volatile boolean flag and checking it regularly
CThread.interrupt() followed by checking isInterrupted()
DBoth B and C are acceptable modern approaches
Correct Answer:
D. Both B and C are acceptable modern approaches
EXPLANATION
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.
Correct Answer:
B. Task 1 printed, then Task 2 printed
EXPLANATION
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.