Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

476 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 291–300 of 476
Topics in Java Programming
Q.291 Medium Multithreading
What will happen if you try to call start() on a thread that has already completed execution?
A The thread will start again
B IllegalThreadStateException will be thrown
C The program will hang
D No error, start() will be ignored
Correct Answer:  B. IllegalThreadStateException will be thrown
EXPLANATION

Once a thread completes (reaches TERMINATED state), calling start() again throws IllegalThreadStateException. A thread can only be started once.

Take Test
Q.292 Medium Multithreading
What is the key difference between Callable and Runnable?
A Callable can return a value and throw checked exceptions; Runnable cannot
B Runnable is faster than Callable
C Callable is used for single-threaded programs
D They are functionally identical
Correct Answer:  A. Callable can return a value and throw checked exceptions; Runnable cannot
EXPLANATION

Callable<V> can return a value of type V and throw checked exceptions. Runnable has void run() method and cannot throw checked exceptions.

Take Test
Q.293 Medium Multithreading
In a deadlock scenario, which of the following is always true?
A Two or more threads are blocked waiting for resources held by each other
B Threads are executing concurrently without issues
C Memory usage increases indefinitely
D The JVM automatically recovers from deadlock
Correct Answer:  A. Two or more threads are blocked waiting for resources held by each other
EXPLANATION

Deadlock occurs when multiple threads are blocked indefinitely, each waiting for a resource held by another, creating a circular wait condition.

Take Test
Q.294 Medium Multithreading
What happens when Thread.interrupt() is called on a thread?
A Sets the interrupt flag; the thread can check it using isInterrupted()
B Immediately terminates the thread
C Pauses the thread execution
D Creates a new interrupt exception
Correct Answer:  A. Sets the interrupt flag; the thread can check it using isInterrupted()
EXPLANATION

interrupt() sets an internal interrupt flag. The thread must check this flag using isInterrupted() or interrupted() and handle it appropriately.

Take Test
Q.295 Medium Multithreading
What is the primary difference between Semaphore and Mutex?
A Semaphore allows multiple threads to access a resource, Mutex allows only one
B Mutex allows multiple threads, Semaphore allows only one
C They are identical
D Semaphore is used for I/O operations only
Correct Answer:  A. Semaphore allows multiple threads to access a resource, Mutex allows only one
EXPLANATION

A Semaphore maintains a count allowing multiple threads (based on permit count) to access a resource. A Mutex (binary semaphore with count=1) allows only one thread.

Take Test
Q.296 Medium Multithreading
In a synchronized block, if notifyAll() is called, what happens?
A All waiting threads are woken up and compete for the lock
B All threads in the application are stopped
C Only one random thread is woken up
D The current thread is immediately terminated
Correct Answer:  A. All waiting threads are woken up and compete for the lock
EXPLANATION

notifyAll() wakes up all threads waiting on the monitor. They will then compete to acquire the lock when it's released.

Take Test
Q.297 Medium Multithreading
What is the purpose of the wait() method in Java?
A To release the lock and make the thread wait until another thread calls notify()
B To pause the thread execution indefinitely
C To create a new thread
D To check if a thread is alive
Correct Answer:  A. To release the lock and make the thread wait until another thread calls notify()
EXPLANATION

wait() releases the monitor lock and causes the thread to wait until notify() or notifyAll() is called by another thread, enabling inter-thread communication.

Take Test
Q.298 Medium Multithreading
Which of the following is true about CountDownLatch?
A It can only be used once and is not reusable
B It can be reset and reused multiple times
C It prevents thread creation
D It is deprecated in Java 21
Correct Answer:  A. It can only be used once and is not reusable
EXPLANATION

CountDownLatch is a one-time use synchronizer. Once the count reaches zero, it cannot be reset. CyclicBarrier is reusable.

Take Test
Q.299 Medium Multithreading
What is CyclicBarrier used for in multithreading?
A To synchronize multiple threads at a common point and reuse the barrier
B To limit the number of threads in an application
C To create a thread pool
D To prevent deadlocks
Correct Answer:  A. To synchronize multiple threads at a common point and reuse the barrier
EXPLANATION

CyclicBarrier allows a set of threads to wait for each other to reach a common point, then all proceed together. It's reusable unlike CountDownLatch.

Take Test
Q.300 Medium Multithreading
What is the output of the following code?
volatile int counter = 0;
counter++; // in multiple threads
Which issue may occur?
A Race condition because counter++ is not atomic
B No issue, volatile ensures thread safety
C Deadlock will occur
D OutOfMemoryError
Correct Answer:  A. Race condition because counter++ is not atomic
EXPLANATION

volatile only ensures visibility, not atomicity. counter++ is actually three operations (read, increment, write), so even with volatile, race conditions can occur.

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