Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 631–640 of 958
Topics in Java Programming
Q.631 Medium Multithreading
Which of the following is a thread-safe collection in Java that can be used without explicit synchronization?
A ArrayList
B HashMap
C ConcurrentHashMap
D TreeMap
Correct Answer:  C. ConcurrentHashMap
EXPLANATION

ConcurrentHashMap is designed for concurrent access and is thread-safe. ArrayList, HashMap, and TreeMap require external synchronization for thread safety.

Take Test
Q.632 Easy Multithreading
What is the return type of the join() method in Java threading?
A Thread
B boolean
C void
D int
Correct Answer:  C. void
EXPLANATION

The join() method returns void. It causes the calling thread to wait until the thread on which it is called completes its execution.

Take Test
Q.633 Medium Multithreading
Consider a scenario where Thread A acquires Lock1 and tries to acquire Lock2, while Thread B acquires Lock2 and tries to acquire Lock1. What situation arises?
A Thread starvation
B Deadlock
C Livelock
D Race condition
Correct Answer:  B. Deadlock
EXPLANATION

When threads wait indefinitely for each other to release resources, it's a deadlock. This scenario is a classic deadlock example.

Take Test
Q.634 Easy Multithreading
Which exception is thrown when a thread is interrupted while waiting?
A ThreadException
B InterruptedException
C WaitException
D SleepException
Correct Answer:  B. InterruptedException
EXPLANATION

InterruptedException is thrown when a thread is interrupted during sleep(), wait(), or join() operations.

Take Test
Q.635 Medium Multithreading
What will be the output of the following code snippet?
java
class Test extends Thread {
public void run() {
System.out.print("T");
}
}
public class Main {
public static void main(String[] args) {
Test t = new Test();
t.run();
t.start();
}
}
A TT
B T
C Compilation error
D Runtime exception
Correct Answer:  A. TT
EXPLANATION

First t.run() executes directly (prints T), then t.start() creates a new thread and calls run() again (prints T). So output is TT.

Take Test
Q.636 Easy Multithreading
Which of the following statements about the notify() method is correct?
A It wakes up all waiting threads simultaneously
B It wakes up only one random waiting thread
C It terminates the current thread immediately
D It pauses the current thread indefinitely
Correct Answer:  B. It wakes up only one random waiting thread
EXPLANATION

notify() wakes up a single arbitrary thread that is waiting on the same monitor. notifyAll() wakes up all waiting threads.

Take Test
Q.637 Easy Multithreading
What is the primary purpose of the synchronized keyword in Java?
A To increase thread execution speed
B To prevent race conditions by ensuring mutual exclusion
C To create new threads automatically
D To handle exceptions in multithreaded environments
Correct Answer:  B. To prevent race conditions by ensuring mutual exclusion
EXPLANATION

The synchronized keyword ensures that only one thread can execute a critical section at a time, preventing race conditions and data inconsistency.

Take Test
Q.638 Easy Multithreading
In Java multithreading, which method is used to pause the execution of a thread for a specified number of milliseconds without releasing locks?
A Thread.sleep()
B Thread.pause()
C Thread.halt()
D Thread.suspend()
Correct Answer:  A. Thread.sleep()
EXPLANATION

Thread.sleep() pauses thread execution for specified milliseconds while retaining all locks. pause(), halt(), and suspend() are not standard Java methods for this purpose.

Take Test
Q.639 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.

Take Test
Q.640 Medium Multithreading
What is thread pooling and which class is commonly used for it?
A Creating individual threads, Thread class
B Reusing a fixed set of threads, ExecutorService
C Synchronizing threads, Synchronized class
D Managing thread priority, ThreadGroup
Correct Answer:  B. Reusing a fixed set of threads, ExecutorService
EXPLANATION

Thread pooling reuses a fixed set of threads instead of creating new ones for each task. ExecutorService provides convenient methods for thread pool management.

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