Home Subjects Java Programming Multithreading

Java Programming
Multithreading

Java OOP, collections, multithreading

50 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 21–30 of 50
Topics in Java Programming
Q.21 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.

Test
Q.22 Medium Multithreading
What happens if an exception is thrown inside a synchronized block?
A The lock is automatically released when exiting the block
B The lock is permanently held
C The exception propagates, but lock is not released
D The thread terminates immediately
Correct Answer:  A. The lock is automatically released when exiting the block
EXPLANATION

Java guarantees that the lock is released when exiting a synchronized block, whether normally or via an exception. This is why synchronized is considered safer than manual lock management.

Test
Q.23 Medium Multithreading
Which method is used to forcefully stop a thread in modern Java?
A Thread.stop() method
B Using a shared volatile boolean flag and checking it regularly
C Thread.interrupt() followed by checking isInterrupted()
D Both B and C are acceptable modern approaches
Correct Answer:  D. Both B and C are acceptable modern approaches
EXPLANATION

Thread.stop() is deprecated and unsafe. Modern approaches use either a volatile flag or Thread.interrupt() with proper handling. Both are valid and recommended depending on the scenario.

Test
Q.24 Medium Multithreading
What is the output of this code?

ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> System.out.println("Task 1"));
executor.submit(() -> System.out.println("Task 2"));
executor.shutdown();
A Task 1 and Task 2 printed in any order
B Task 1 printed, then Task 2 printed
C No output
D Compilation error
Correct Answer:  B. Task 1 printed, then Task 2 printed
EXPLANATION

newSingleThreadExecutor() creates an executor with exactly one thread. Tasks are queued and executed sequentially in the order submitted. Task 1 will always execute before Task 2.

Test
Q.25 Medium Multithreading
In a scenario where Thread A holds Lock1 and waits for Lock2, while Thread B holds Lock2 and waits for Lock1, what is this called?
A Race condition
B Deadlock
C Starvation
D Livelock
Correct Answer:  B. Deadlock
EXPLANATION

This is a classic deadlock scenario involving circular wait. Race condition involves competing for resources. Starvation is when a thread never gets the resource. Livelock is when threads keep changing states without progress.

Test
Q.26 Medium Multithreading
What happens when a thread acquires a lock on an object and then calls wait()?
A The thread continues holding the lock and waits
B The thread releases the lock and enters the waiting pool
C The thread is terminated
D An IllegalMonitorStateException is thrown immediately
Correct Answer:  B. The thread releases the lock and enters the waiting pool
EXPLANATION

When wait() is called, the thread releases the lock it holds on the object and enters the waiting pool. Another thread can then acquire the lock. The thread re-acquires the lock when notified.

Test
Q.27 Medium Multithreading
Which of the following best describes CyclicBarrier?
A It allows N threads to wait for each other at a barrier point
B It decrements a counter until it reaches zero
C It prevents threads from accessing a shared resource
D It executes a task periodically in a separate thread
Correct Answer:  A. It allows N threads to wait for each other at a barrier point
EXPLANATION

CyclicBarrier is a synchronizer that allows a fixed number of threads to wait for each other at a barrier point. Once all threads reach the barrier, they proceed together. It's reusable (cyclic) unlike CountDownLatch.

Test
Q.28 Medium Multithreading
What is the difference between notify() and notifyAll() in Java?
A notify() wakes one waiting thread; notifyAll() wakes all waiting threads
B They are identical and can be used interchangeably
C notify() is for synchronized methods; notifyAll() is for synchronized blocks
D notifyAll() is deprecated in Java 21
Correct Answer:  A. notify() wakes one waiting thread; notifyAll() wakes all waiting threads
EXPLANATION

notify() randomly selects one waiting thread to wake up, while notifyAll() wakes all waiting threads. notifyAll() is generally safer to avoid missed notifications when multiple threads are waiting on the same condition.

Test
Q.29 Medium Multithreading
Which interface would you use to submit multiple tasks and wait for all of them to complete?
A Runnable
B Callable with ExecutorService.invokeAll()
C Thread
D Semaphore
Correct Answer:  B. Callable with ExecutorService.invokeAll()
EXPLANATION

Callable interface returns a result via Future, and ExecutorService.invokeAll() waits for all submitted tasks to complete. This is the standard pattern for parallel task execution with result collection.

Test
Q.30 Medium Multithreading
What will be the output of the following code?

Object lock = new Object();
synchronized(lock) {
synchronized(lock) {
System.out.println("Nested");
}
}
A Will compile and print 'Nested' successfully
B Will cause a deadlock
C Will throw an IllegalMonitorStateException
D Will throw a CompileTimeException
Correct Answer:  A. Will compile and print 'Nested' successfully
EXPLANATION

Java supports reentrant locks on synchronized blocks. The same thread can acquire the same lock multiple times. The lock is released only when the outermost synchronized block exits.

Test
IGET
IGET AI
Online · Exam prep assistant
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