Consider code where Thread A holds Lock1 and waits for Lock2, while Thread B holds Lock2 and waits for Lock1. What is this scenario called?
ARace condition
BStarvation
CDeadlock
DLivelock
Correct Answer:
C. Deadlock
EXPLANATION
This is a classic circular wait scenario resulting in deadlock, where neither thread can proceed because each is waiting for a resource held by the other.
What is thread pooling and which class is commonly used for it?
ACreating individual threads, Thread class
BReusing a fixed set of threads, ExecutorService
CSynchronizing threads, Synchronized class
DManaging thread priority, ThreadGroup
Correct Answer:
B. Reusing a fixed set of threads, ExecutorService
EXPLANATION
Thread pooling reuses a fixed set of threads instead of creating new ones for each task. ExecutorService provides convenient methods for thread pool management.
What will happen if you call sleep() inside a synchronized block?
AThe lock is released during sleep
BThe lock is retained during sleep, blocking other threads
CAn exception is thrown
DThe synchronized block is skipped
Correct Answer:
B. The lock is retained during sleep, blocking other threads
EXPLANATION
When sleep() is called inside a synchronized block, the thread retains the lock on the object, preventing other threads from entering the synchronized section.
How does the volatile keyword help in multithreading?
AIt prevents race conditions completely
BIt ensures visibility of variable changes across threads without full synchronization
CIt increases thread speed
DIt prevents deadlocks
Correct Answer:
B. It ensures visibility of variable changes across threads without full synchronization
EXPLANATION
The volatile keyword ensures that changes to a variable are immediately visible to all threads, providing memory visibility without the overhead of full synchronization.
AA thread that runs in the background and doesn't prevent JVM shutdown
BA thread that has the highest priority
CA thread that cannot be interrupted
DA thread that runs system services
Correct Answer:
A. A thread that runs in the background and doesn't prevent JVM shutdown
EXPLANATION
A daemon thread runs in the background and doesn't prevent the JVM from exiting. When all non-daemon threads finish, the JVM exits even if daemon threads are running.