Which interface must be implemented to create a thread in Java?
Answer: A
The Runnable interface is the correct interface to implement for creating threads in Java. It has a single abstract method run().
Q.2Easy
Which method is used to stop a thread in Java?
Answer: B
The interrupt() method is the proper way to stop a thread in Java. The stop() method is deprecated and dangerous.
Q.3Easy
Which method is called to make a thread wait until another thread completes?
Answer: B
The join() method causes the current thread to wait until the thread on which it is called completes its execution.
Q.4Easy
Which of the following is a valid way to create a thread in Java?
Answer: C
Both extending Thread class and implementing Runnable interface are valid approaches to create threads in Java.
Q.5Easy
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.
Advertisement
Q.6Easy
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.7Easy
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.8Easy
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.9Easy
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.10Easy
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.11Easy
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.12Easy
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.
Q.13Easy
Which collection class is thread-safe without explicit synchronization?
Answer: C
ConcurrentHashMap uses segment-based locking (in older versions) or fine-grained locking to provide thread-safety without synchronizing the entire map. Other options require external synchronization or Collections.synchronizedMap().
Q.14Easy
Which method is used to prevent race conditions by allowing only one thread to access a resource at a time?
Answer: A
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.
Q.15Easy
Which of the following collections is thread-safe in Java?
Answer: A
ConcurrentHashMap is designed for concurrent access without requiring external synchronization. HashMap and others are not thread-safe.
Q.16Easy
What is the output of the following code?
Thread t = new Thread(() -> System.out.println(Thread.currentThread().getName()));
t.start();
Answer: A
When a new Thread is created without a name parameter, it gets a default name 'Thread-n' where n is a counter. The thread will print 'Thread-0' as it is the first thread created.
Q.17Easy
Which method must be implemented to create a thread in Java?
Answer: B
The run() method must be implemented when creating a thread either by extending Thread class or implementing Runnable interface. The start() method calls run() internally.
Q.18Easy
What is the difference between start() and run() methods in threading?
Answer: A
start() creates a new thread and calls run() in that new thread, while directly calling run() executes it in the current thread without creating a new thread.
Q.19Easy
Which of the following thread states is NOT a valid Java thread state?
Answer: C
Java thread states are: NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, and TERMINATED. SUSPENDED is not a valid state in Java's threading model.
Q.20Easy
Which of the following methods will cause a thread to release all locks it holds while waiting?
Answer: A
The wait() method causes a thread to release all acquired locks and enter a waiting state until notify() or notifyAll() is called. sleep(), yield(), and join() do not release locks.