Java interviews and campus tests lean heavily on object oriented behaviour rather than API recall. This set covers inheritance and polymorphism, interfaces and abstract classes, the collections framework, exception handling, multithreading, string handling, and JVM basics such as garbage collection. Output based questions are included because they expose the gaps that theory questions hide.
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.302Easy
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.303Medium
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.304Medium
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.305Medium
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.306Easy
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.307Medium
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.308Medium
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.309Medium
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.310Medium
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.311Hard
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.312Medium
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.313Easy
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.314Medium
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.315Hard
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.316Hard
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.317Hard
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.318Medium
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.319Hard
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.
Q.320Easy
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.