Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 601–610 of 958
Topics in Java Programming
Q.601 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.602 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.603 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.604 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.605 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.606 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.607 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.608 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
Q.609 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.

Take Test
Q.610 Easy Multithreading
Which collection class is thread-safe without explicit synchronization?
A ArrayList
B HashMap
C ConcurrentHashMap
D TreeMap
Correct Answer:  C. ConcurrentHashMap
EXPLANATION

ConcurrentHashMap uses segment-based locking (in older versions) or fine-grained locking to provide thread-safety without synchronizing the entire map. Other options require external synchronization or Collections.synchronizedMap().

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