Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 591–600 of 958
Topics in Java Programming
Q.591 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.592 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.593 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.594 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.595 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.596 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.597 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.598 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
Q.599 Medium Multithreading
In Java 21 Virtual Threads, what is the key advantage over platform threads?
A Virtual threads are lighter weight and millions can be created without OS limitations
B Virtual threads are faster than platform threads
C Virtual threads do not require synchronization
D Virtual threads cannot cause deadlocks
Correct Answer:  A. Virtual threads are lighter weight and millions can be created without OS limitations
EXPLANATION

Virtual threads (Project Loom) are extremely lightweight and allow creating millions of threads efficiently, unlike platform threads which are limited by OS resources.

Take Test
Q.600 Easy Multithreading
Which method is used to prevent race conditions by allowing only one thread to access a resource at a time?
A synchronized
B volatile
C atomic
D transient
Correct Answer:  A. synchronized
EXPLANATION

The synchronized keyword creates a critical section that only one thread can access at a time, preventing race conditions. volatile ensures visibility but not atomicity.

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