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.362Medium
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.363Medium
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.364Medium
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.365Medium
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.
Advertisement
Q.366Hard
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.367Medium
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.368Easy
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.369Medium
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.370Hard
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.371Hard
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.372Hard
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.373Easy
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.374Easy
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.375Easy
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.376Easy
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.377Medium
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.378Medium
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.379Medium
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.
Q.380Medium
Which Java class provides thread-safe operations using Compare-And-Swap (CAS)?
Answer: C
AtomicInteger and other Atomic* classes use CAS operations for lock-free thread-safe operations. ConcurrentHashMap uses segment-based locking, not CAS.