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.2Medium
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.3Easy
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.4Medium
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.5Medium
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.
Advertisement
Q.6Medium
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.7Easy
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.8Medium
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.
Q.9Medium
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.10Medium
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.11Medium
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.12Hard
What happens when wait() is called from a thread that doesn't hold the lock?
Answer: B
wait() must be called from within a synchronized context. Calling it without holding the lock throws IllegalMonitorStateException.
Q.13Medium
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.14Easy
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.15Medium
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.16Hard
How does the volatile keyword help in multithreading?
Answer: B
The volatile keyword ensures that changes to a variable are immediately visible to all threads, providing memory visibility without the overhead of full synchronization.
Q.17Hard
What will happen if you call sleep() inside a synchronized block?
Answer: B
When sleep() is called inside a synchronized block, the thread retains the lock on the object, preventing other threads from entering the synchronized section.
Q.18Hard
Which interface allows multiple threads to access a resource with a permit system?
Answer: B
Semaphore is a synchronization utility that uses a counter to control access to a shared resource. Threads can acquire and release permits.
Q.19Medium
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.20Hard
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?
Answer: C
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.