Java Programming — Multithreading
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 31–40 of 100 questions in Multithreading
Q.31 Medium Multithreading
In a deadlock scenario, which of the following is always true?
A Two or more threads are blocked waiting for resources held by each other
B Threads are executing concurrently without issues
C Memory usage increases indefinitely
D The JVM automatically recovers from deadlock
Correct Answer:  A. Two or more threads are blocked waiting for resources held by each other
EXPLANATION

Deadlock occurs when multiple threads are blocked indefinitely, each waiting for a resource held by another, creating a circular wait condition.

Take Test
Q.32 Hard Multithreading
Consider a scenario: Thread A is waiting in wait() inside a synchronized block. Thread B calls notify(). What is the state of Thread A?
A Thread A transitions to runnable state after acquiring the lock
B Thread A immediately starts executing
C Thread A remains blocked permanently
D Thread A is terminated
Correct Answer:  A. Thread A transitions to runnable state after acquiring the lock
EXPLANATION

After notify(), the waiting thread transitions to runnable state, but it must still acquire the lock before resuming execution within the synchronized block.

Take Test
Q.33 Medium Multithreading
What happens when Thread.interrupt() is called on a thread?
A Sets the interrupt flag; the thread can check it using isInterrupted()
B Immediately terminates the thread
C Pauses the thread execution
D Creates a new interrupt exception
Correct Answer:  A. Sets the interrupt flag; the thread can check it using isInterrupted()
EXPLANATION

interrupt() sets an internal interrupt flag. The thread must check this flag using isInterrupted() or interrupted() and handle it appropriately.

Take Test
Q.34 Medium Multithreading
What is the primary difference between Semaphore and Mutex?
A Semaphore allows multiple threads to access a resource, Mutex allows only one
B Mutex allows multiple threads, Semaphore allows only one
C They are identical
D Semaphore is used for I/O operations only
Correct Answer:  A. Semaphore allows multiple threads to access a resource, Mutex allows only one
EXPLANATION

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.

Take Test
Q.35 Medium Multithreading
In a synchronized block, if notifyAll() is called, what happens?
A All waiting threads are woken up and compete for the lock
B All threads in the application are stopped
C Only one random thread is woken up
D The current thread is immediately terminated
Correct Answer:  A. All waiting threads are woken up and compete for the lock
EXPLANATION

notifyAll() wakes up all threads waiting on the monitor. They will then compete to acquire the lock when it's released.

Take Test
Q.36 Medium Multithreading
What is the purpose of the wait() method in Java?
A To release the lock and make the thread wait until another thread calls notify()
B To pause the thread execution indefinitely
C To create a new thread
D To check if a thread is alive
Correct Answer:  A. To release the lock and make the thread wait until another thread calls notify()
EXPLANATION

wait() releases the monitor lock and causes the thread to wait until notify() or notifyAll() is called by another thread, enabling inter-thread communication.

Take Test
Q.37 Medium Multithreading
Which of the following is true about CountDownLatch?
A It can only be used once and is not reusable
B It can be reset and reused multiple times
C It prevents thread creation
D It is deprecated in Java 21
Correct Answer:  A. It can only be used once and is not reusable
EXPLANATION

CountDownLatch is a one-time use synchronizer. Once the count reaches zero, it cannot be reset. CyclicBarrier is reusable.

Take Test
Q.38 Medium Multithreading
What is CyclicBarrier used for in multithreading?
A To synchronize multiple threads at a common point and reuse the barrier
B To limit the number of threads in an application
C To create a thread pool
D To prevent deadlocks
Correct Answer:  A. To synchronize multiple threads at a common point and reuse the barrier
EXPLANATION

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.

Take Test
Q.39 Medium Multithreading
What is the output of the following code?
volatile int counter = 0;
counter++; // in multiple threads
Which issue may occur?
A Race condition because counter++ is not atomic
B No issue, volatile ensures thread safety
C Deadlock will occur
D OutOfMemoryError
Correct Answer:  A. Race condition because counter++ is not atomic
EXPLANATION

volatile only ensures visibility, not atomicity. counter++ is actually three operations (read, increment, write), so even with volatile, race conditions can occur.

Take Test
Q.40 Medium Multithreading
What does the volatile keyword guarantee in Java multithreading?
A Visibility of changes across threads, but not atomicity
B Both visibility and atomicity
C Prevents thread creation
D Ensures mutual exclusion
Correct Answer:  A. Visibility of changes across threads, but not atomicity
EXPLANATION

volatile ensures that all threads see the most recent value of a variable (visibility) but does not provide atomicity for compound operations.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips