What is the difference between Thread.sleep() and Thread.yield()?
Answer: C
Thread.sleep() pauses execution for a specified milliseconds, while yield() is a hint to the scheduler that the current thread is willing to yield its turn.
Q.2Medium
What is the purpose of the synchronized keyword in Java multithreading?
Answer: B
The synchronized keyword provides mutual exclusion to ensure that only one thread can access a resource at a time, preventing race conditions.
Q.3Medium
Which of the following is NOT a thread state in Java?
Answer: D
The thread states are NEW, RUNNABLE, RUNNING, BLOCKED, WAITING, TIMED_WAITING, and TERMINATED. SLEEPING is not an official state.
Q.4Medium
What will be the output of calling start() multiple times on the same thread object?
Answer: B
Calling start() on an already started thread throws IllegalThreadStateException because a thread can only be started once.
Q.5Medium
What is a race condition in multithreading?
Answer: B
A race condition occurs when multiple threads access shared data and the final result depends on the order of execution, which is unpredictable.
Advertisement
Q.6Medium
Consider a synchronized method. Can multiple threads call it simultaneously on the same object?
Answer: B
A synchronized method locks the object, allowing only one thread to execute it at a time for that object instance.
Q.7Medium
What is the difference between notify() and notifyAll()?
Answer: A
notify() wakes up a single thread that is waiting on the object's monitor, while notifyAll() wakes up all threads waiting on that object.
Q.8Medium
Which class provides thread-safe operations on shared variables in Java?
Answer: C
The AtomicInteger class (and other Atomic classes) provides thread-safe operations without explicit synchronization using low-level atomic operations.
Q.9Medium
What is a daemon thread in Java?
Answer: A
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.
Q.10Medium
What is a deadlock in multithreading?
Answer: B
A deadlock occurs when two or more threads are blocked forever, waiting for each other to release resources they need.
Q.11Medium
What is thread pooling and which class is commonly used for it?
Answer: B
Thread pooling reuses a fixed set of threads instead of creating new ones for each task. ExecutorService provides convenient methods for thread pool management.
Q.12Medium
What will be the output of the following code snippet?
java
class Test extends Thread {
public void run() {
System.out.print("T");
}
}
public class Main {
public static void main(String[] args) {
Test t = new Test();
t.run();
t.start();
}
}
Answer: A
First t.run() executes directly (prints T), then t.start() creates a new thread and calls run() again (prints T). So output is TT.
Q.13Medium
Consider a scenario where Thread A acquires Lock1 and tries to acquire Lock2, while Thread B acquires Lock2 and tries to acquire Lock1. What situation arises?
Answer: B
When threads wait indefinitely for each other to release resources, it's a deadlock. This scenario is a classic deadlock example.
Q.14Medium
Which of the following is a thread-safe collection in Java that can be used without explicit synchronization?
Answer: C
ConcurrentHashMap is designed for concurrent access and is thread-safe. ArrayList, HashMap, and TreeMap require external synchronization for thread safety.
Q.15Medium
Which method is used to check the current state of a thread in Java?
Answer: B
The getState() method returns the Thread.State enum indicating whether a thread is NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, or TERMINATED.
Q.16Medium
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.17Medium
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.18Medium
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.19Medium
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.20Medium
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.