In Java multithreading, which method is used to pause the execution of a thread for a specified number of milliseconds without releasing locks?
Answer: A
Thread.sleep() pauses thread execution for specified milliseconds while retaining all locks. pause(), halt(), and suspend() are not standard Java methods for this purpose.
Q.22Easy
Which of the following statements about the notify() method is correct?
Answer: B
notify() wakes up a single arbitrary thread that is waiting on the same monitor. notifyAll() wakes up all waiting threads.
Q.23Medium
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.24Easy
Which exception is thrown when a thread is interrupted while waiting?
Answer: B
InterruptedException is thrown when a thread is interrupted during sleep(), wait(), or join() operations.
Q.25Medium
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.
Advertisement
Q.26Easy
What is the return type of the join() method in Java threading?
Answer: C
The join() method returns void. It causes the calling thread to wait until the thread on which it is called completes its execution.
Q.27Medium
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.28Medium
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.29Medium
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.30Medium
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.31Hard
Consider the following code. What will be the likely behavior?
java
synchronized void method1() { method2(); }
synchronized void method2() { }
Answer: C
Java's intrinsic locks are reentrant, meaning the same thread can acquire the same lock multiple times. The thread can call method2() from method1() without deadlock.
Q.32Hard
Which class is used to synchronize the execution of multiple threads at a specific point?
Answer: B
CyclicBarrier allows multiple threads to wait for each other at a specific point. CountDownLatch is for one-time synchronization; CyclicBarrier is reusable.
Q.33Hard
What is the difference between CountDownLatch and CyclicBarrier?
Answer: B
CountDownLatch is used for one-time synchronization where threads wait for a countdown to reach zero. CyclicBarrier can be reused after threads are released.
Q.34Medium
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.35Hard
What will happen if a thread tries to acquire a lock it already holds while inside a synchronized method?
Answer: C
Java's monitors are reentrant. A thread can acquire the same lock multiple times. An internal counter tracks lock acquisitions and releases.
Q.36Easy
Which of the following best describes the purpose of the Runnable interface in Java?
Answer: B
The Runnable interface contains a single run() method that defines the code to be executed when a thread runs. It's preferred over extending Thread class.
Q.37Hard
In a high-concurrency scenario with 2024-25 exam patterns, which Java construct provides the best lock-free approach for thread safety?
Answer: C
Atomic variables use Compare-And-Swap (CAS) operations for lock-free thread safety, providing better performance in high-concurrency scenarios compared to traditional locking.
Q.38Easy
Which of the following statements about Java threads is correct?
Answer: C
A thread can only exist in one state at a time (NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, or TERMINATED). Calling start() multiple times throws IllegalThreadStateException. The start() method should be called, not run() directly.
Q.39Easy
What is the primary purpose of the volatile keyword in Java multithreading?
Answer: B
The volatile keyword ensures that any read of a volatile variable will read the most recent write by any thread. It provides visibility guarantees but not atomicity, making it lighter than synchronization for simple flag checks.
Q.40Easy
Which of the following will cause a deadlock situation?
Answer: A
Deadlock occurs when two or more threads are waiting indefinitely for locks held by each other. Option B creates a reentrant situation (allowed in ReentrantLock). Option C is problematic but not necessarily deadlock. Option D is safe if used properly.