Java Programming — Multithreading
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 41–50 of 100 questions in Multithreading
Q.41 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.42 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
Q.43 Hard Multithreading
If a thread is blocked waiting for I/O, what happens when interrupt() is called on it?
A The thread immediately resumes execution
B InterruptedException is thrown if the I/O operation supports interruption
C The thread state changes but I/O continues
D Nothing happens; interrupt() doesn't affect blocking I/O
Correct Answer:  B. InterruptedException is thrown if the I/O operation supports interruption
EXPLANATION

Calling interrupt() on a blocked thread sets the interrupt flag. If the blocking operation supports interruption (like sleep(), join(), wait()), it throws InterruptedException. Non-interruptible I/O blocks require other mechanisms.

Take Test
Q.44 Hard Multithreading
What is the purpose of ForkJoinPool in Java?
A To handle HTTP requests in parallel
B To divide work recursively using divide-and-conquer approach
C To prevent thread creation
D To serialize data across threads
Correct Answer:  B. To divide work recursively using divide-and-conquer approach
EXPLANATION

ForkJoinPool is designed for divide-and-conquer tasks where large problems are split (fork) into smaller subtasks and results are combined (join). It's optimized for recursive parallel algorithms.

Take Test
Q.45 Hard Multithreading
Which pattern should be used to safely publish data from one thread to another?
A Directly assign to a public static variable
B Use synchronized, volatile, or thread-safe collections like ConcurrentHashMap
C Use Thread.sleep() to ensure visibility
D No special mechanism is needed in Java
Correct Answer:  B. Use synchronized, volatile, or thread-safe collections like ConcurrentHashMap
EXPLANATION

Safe publication requires using visibility mechanisms: synchronized (mutual exclusion), volatile (visibility), or thread-safe collections (both). Direct assignment without synchronization causes visibility issues in the memory model.

Take Test
Q.46 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.

Take Test
Q.47 Hard Multithreading
In a high-traffic web application using Java 21 Virtual Threads, what is the main performance benefit?
A Virtual Threads eliminate the need for synchronization
B Virtual Threads reduce memory overhead per thread, allowing millions of concurrent connections
C Virtual Threads guarantee no deadlocks
D Virtual Threads make single-threaded code faster
Correct Answer:  B. Virtual Threads reduce memory overhead per thread, allowing millions of concurrent connections
EXPLANATION

Virtual Threads are lightweight (millions can run) with minimal memory overhead compared to platform threads (thousands). They automatically handle I/O blocking without thread creation, making them ideal for high-concurrency scenarios.

Take Test
Q.48 Hard Multithreading
What is the primary advantage of using ReentrantLock over synchronized?
A ReentrantLock is always faster
B ReentrantLock allows conditional waiting with Condition objects and tryLock()
C ReentrantLock prevents all deadlocks automatically
D ReentrantLock works with primitives
Correct Answer:  B. ReentrantLock allows conditional waiting with Condition objects and tryLock()
EXPLANATION

ReentrantLock provides more control with tryLock() for non-blocking attempts, Condition objects for complex wait/notify patterns, and fairness policy. It's more flexible than synchronized.

Take Test
Q.49 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.

Take Test
Q.50 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.

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