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.
CyclicBarrier allows a set of threads to wait for each other to reach a common point, then all proceed together. It's reusable unlike CountDownLatch.
Q.62Medium
Which of the following is true about CountDownLatch?
Answer: A
CountDownLatch is a one-time use synchronizer. Once the count reaches zero, it cannot be reset. CyclicBarrier is reusable.
Q.63Medium
What is the purpose of the wait() method in Java?
Answer: A
wait() releases the monitor lock and causes the thread to wait until notify() or notifyAll() is called by another thread, enabling inter-thread communication.
Q.64Medium
In a synchronized block, if notifyAll() is called, what happens?
Answer: A
notifyAll() wakes up all threads waiting on the monitor. They will then compete to acquire the lock when it's released.
Q.65Medium
What is the primary difference between Semaphore and Mutex?
Answer: A
A Semaphore maintains a count allowing multiple threads (based on permit count) to access a resource. A Mutex (binary semaphore with count=1) allows only one thread.
Q.66Medium
What happens when Thread.interrupt() is called on a thread?
Answer: A
interrupt() sets an internal interrupt flag. The thread must check this flag using isInterrupted() or interrupted() and handle it appropriately.
Q.67Hard
Consider a scenario: Thread A is waiting in wait() inside a synchronized block. Thread B calls notify(). What is the state of Thread A?
Answer: A
After notify(), the waiting thread transitions to runnable state, but it must still acquire the lock before resuming execution within the synchronized block.
Q.68Medium
In a deadlock scenario, which of the following is always true?
Answer: A
Deadlock occurs when multiple threads are blocked indefinitely, each waiting for a resource held by another, creating a circular wait condition.
Q.69Easy
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.70Medium
What is the key difference between Callable and Runnable?
Answer: A
Callable<V> can return a value of type V and throw checked exceptions. Runnable has void run() method and cannot throw checked exceptions.
Q.71Hard
In a high-concurrency scenario using Java 21, which approach is recommended for I/O-bound operations?
Answer: A
Virtual Threads (Project Loom) are ideal for I/O-bound operations as they have minimal overhead and can number in millions, improving scalability significantly.
Q.72Hard
What is the purpose of the strictfp modifier in the context of multithreading?
Answer: A
strictfp ensures consistent floating-point results across different platforms/JVMs, though it's not directly a multithreading construct.
Q.73Hard
In a producer-consumer problem, what is the ideal synchronization mechanism?
Answer: A
BlockingQueue (like LinkedBlockingQueue) is purpose-built for producer-consumer patterns, handling synchronization and blocking elegantly.
Q.74Easy
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.75Easy
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.76Easy
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.77Easy
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.78Medium
What will happen if you try to call start() on a thread that has already completed execution?
Answer: B
Once a thread completes (reaches TERMINATED state), calling start() again throws IllegalThreadStateException. A thread can only be started once.
Q.79Medium
Consider the code: synchronized void method1() { wait(); } If wait() is called without a lock, what happens?
Answer: B
wait() must be called from within a synchronized block or method. If called outside synchronized context, it throws IllegalMonitorStateException at runtime.
Q.80Medium
What is the purpose of the yield() method in Java threading?
Answer: B
yield() is a hint to the thread scheduler that the current thread is willing to yield its current use of CPU. It doesn't guarantee the thread will yield, as scheduling is JVM-dependent.