Home Subjects Java Programming Multithreading

Java Programming
Multithreading

Java OOP, collections, multithreading

28 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 11–20 of 28
Topics in Java Programming
Q.11 Hard Multithreading
What is the purpose of the strictfp modifier in the context of multithreading?
A Ensures consistent floating-point calculations across platforms
B Prevents race conditions
C Forces strict thread scheduling
D It has no relevance to multithreading
Correct Answer:  A. Ensures consistent floating-point calculations across platforms
EXPLANATION

strictfp ensures consistent floating-point results across different platforms/JVMs, though it's not directly a multithreading construct.

Test
Q.12 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.

Test
Q.13 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.

Test
Q.14 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.

Test
Q.15 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.

Test
Q.16 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.

Test
Q.17 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.

Test
Q.18 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.

Test
Q.19 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.

Test
Q.20 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.

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