Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

212 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 131–140 of 212
Topics in Java Programming
Q.131 Hard Multithreading
In a high-concurrency scenario using Java 21, which approach is recommended for I/O-bound operations?
A Virtual Threads with structured concurrency
B Traditional platform threads with thread pools
C Callbacks without threading
D Busy-waiting loops
Correct Answer:  A. Virtual Threads with structured concurrency
EXPLANATION

Virtual Threads (Project Loom) are ideal for I/O-bound operations as they have minimal overhead and can number in millions, improving scalability significantly.

Take Test
Q.132 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.133 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.134 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.135 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.136 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.137 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.138 Hard Multithreading
In a high-concurrency scenario with 2024-25 exam patterns, which Java construct provides the best lock-free approach for thread safety?
A synchronized blocks
B ReentrantReadWriteLock
C AtomicInteger and similar atomic variables
D synchronized collections
Correct Answer:  C. AtomicInteger and similar atomic variables
EXPLANATION

Atomic variables use Compare-And-Swap (CAS) operations for lock-free thread safety, providing better performance in high-concurrency scenarios compared to traditional locking.

Take Test
Q.139 Hard Multithreading
What will happen if a thread tries to acquire a lock it already holds while inside a synchronized method?
A The thread will be blocked indefinitely
B An exception will be thrown
C The thread will successfully acquire the lock again (reentrant behavior)
D The thread will skip the synchronized block
Correct Answer:  C. The thread will successfully acquire the lock again (reentrant behavior)
EXPLANATION

Java's monitors are reentrant. A thread can acquire the same lock multiple times. An internal counter tracks lock acquisitions and releases.

Take Test
Q.140 Hard Multithreading
What is the difference between CountDownLatch and CyclicBarrier?
A No functional difference
B CountDownLatch is one-time use; CyclicBarrier is reusable
C CyclicBarrier is deprecated in Java 2024
D CountDownLatch works with ExecutorService only
Correct Answer:  B. CountDownLatch is one-time use; CyclicBarrier is reusable
EXPLANATION

CountDownLatch is used for one-time synchronization where threads wait for a countdown to reach zero. CyclicBarrier can be reused after threads are released.

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