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 21–28 of 28
Topics in Java Programming
Q.21 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.

Test
Q.22 Hard Multithreading
Which class is used to synchronize the execution of multiple threads at a specific point?
A Semaphore
B CyclicBarrier
C CountDownLatch
D Condition
Correct Answer:  B. CyclicBarrier
EXPLANATION

CyclicBarrier allows multiple threads to wait for each other at a specific point. CountDownLatch is for one-time synchronization; CyclicBarrier is reusable.

Test
Q.23 Hard Multithreading
Consider the following code. What will be the likely behavior?
java
synchronized void method1() { method2(); }
synchronized void method2() { }
A Deadlock will occur
B ReentrantException will be thrown
C Code will execute without issues due to reentrant locks
D Compilation error
Correct Answer:  C. Code will execute without issues due to reentrant locks
EXPLANATION

Java's intrinsic locks are reentrant, meaning the same thread can acquire the same lock multiple times. The thread can call method2() from method1() without deadlock.

Test
Q.24 Hard Multithreading
Consider code where Thread A holds Lock1 and waits for Lock2, while Thread B holds Lock2 and waits for Lock1. What is this scenario called?
A Race condition
B Starvation
C Deadlock
D Livelock
Correct Answer:  C. Deadlock
EXPLANATION

This is a classic circular wait scenario resulting in deadlock, where neither thread can proceed because each is waiting for a resource held by the other.

Test
Q.25 Hard Multithreading
Which interface allows multiple threads to access a resource with a permit system?
A Runnable
B Semaphore
C Lock
D Executor
Correct Answer:  B. Semaphore
EXPLANATION

Semaphore is a synchronization utility that uses a counter to control access to a shared resource. Threads can acquire and release permits.

Test
Q.26 Hard Multithreading
What will happen if you call sleep() inside a synchronized block?
A The lock is released during sleep
B The lock is retained during sleep, blocking other threads
C An exception is thrown
D The synchronized block is skipped
Correct Answer:  B. The lock is retained during sleep, blocking other threads
EXPLANATION

When sleep() is called inside a synchronized block, the thread retains the lock on the object, preventing other threads from entering the synchronized section.

Test
Q.27 Hard Multithreading
How does the volatile keyword help in multithreading?
A It prevents race conditions completely
B It ensures visibility of variable changes across threads without full synchronization
C It increases thread speed
D It prevents deadlocks
Correct Answer:  B. It ensures visibility of variable changes across threads without full synchronization
EXPLANATION

The volatile keyword ensures that changes to a variable are immediately visible to all threads, providing memory visibility without the overhead of full synchronization.

Test
Q.28 Hard Multithreading
What happens when wait() is called from a thread that doesn't hold the lock?
A The thread waits indefinitely
B IllegalMonitorStateException is thrown
C The program exits
D The thread continues normally
Correct Answer:  B. IllegalMonitorStateException is thrown
EXPLANATION

wait() must be called from within a synchronized context. Calling it without holding the lock throws IllegalMonitorStateException.

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