What is the primary difference between Semaphore and Mutex?
Answer: A
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.
Q.182Medium
What happens when Thread.interrupt() is called on a thread?
Answer: A
interrupt() sets an internal interrupt flag. The thread must check this flag using isInterrupted() or interrupted() and handle it appropriately.
Q.183Medium
In a deadlock scenario, which of the following is always true?
Answer: A
Deadlock occurs when multiple threads are blocked indefinitely, each waiting for a resource held by another, creating a circular wait condition.
Q.184Medium
What is the key difference between Callable and Runnable?
Answer: A
Callable<V> can return a value of type V and throw checked exceptions. Runnable has void run() method and cannot throw checked exceptions.
Q.185Medium
What will happen if you try to call start() on a thread that has already completed execution?
Answer: B
Once a thread completes (reaches TERMINATED state), calling start() again throws IllegalThreadStateException. A thread can only be started once.
Advertisement
Q.186Medium
Consider the code:
synchronized void method1() { wait(); }
If wait() is called without a lock, what happens?
Answer: B
wait() must be called from within a synchronized block or method. If called outside synchronized context, it throws IllegalMonitorStateException at runtime.
Q.187Medium
What is the purpose of the yield() method in Java threading?
Answer: B
yield() is a hint to the thread scheduler that the current thread is willing to yield its current use of CPU. It doesn't guarantee the thread will yield, as scheduling is JVM-dependent.
Q.188Medium
Which Java class provides thread-safe operations using Compare-And-Swap (CAS)?
Answer: C
AtomicInteger and other Atomic* classes use CAS operations for lock-free thread-safe operations. ConcurrentHashMap uses segment-based locking, not CAS.
Q.189Medium
What does the volatile keyword guarantee in multithreading?
Answer: B
volatile ensures that changes to a variable are immediately visible to all threads and prevents the JVM from reordering instructions. However, it doesn't make operations atomic.
Q.190Medium
In Java 21, which new feature was introduced for concurrent programming?
Answer: A
Java 21 introduced Virtual Threads as part of Project Loom, which are lightweight threads that make it easier to write scalable concurrent applications.
Q.191Medium
What is the output of this code snippet?
ExecutorService es = Executors.newFixedThreadPool(2);
es.execute(() -> System.out.println("Task 1"));
es.shutdown();
Answer: A
shutdown() gracefully shuts down the executor service by rejecting new tasks but allowing submitted tasks to complete. Task 1 is already submitted before shutdown(), so it will execute.
Q.192Medium
What exception does CyclicBarrier throw when a thread is interrupted while waiting?
Answer: A
When a thread waiting at a CyclicBarrier is interrupted, it throws InterruptedException. This breaks the barrier and causes other waiting threads to receive BrokenBarrierException.
Q.193Medium
In a producer-consumer scenario using BlockingQueue, what happens when a consumer thread calls take() on an empty queue?
Answer: B
BlockingQueue's take() method blocks the calling thread until an element becomes available. This is safer and more efficient than busy-waiting. NoSuchElementException is thrown by non-blocking operations like remove().
Q.194Medium
A multi-threaded application experiences poor performance despite having adequate CPU cores. Code inspection reveals frequent calls to synchronized blocks on shared objects. Which modern Java feature (2024-25) could optimize this without major refactoring?
Answer: A
Virtual Threads (Project Loom, stable in Java 21+) allow massive parallelism with lightweight threads, reducing contention overhead. They enable better scaling than increasing pool size. volatile doesn't help with synchronized block contention, and static conversion is inappropriate.
Q.195Medium
What is the output of the following code?
int x = 10;
try {
x = x / 0;
} catch(ArithmeticException e) {
x = x + 5;
} finally {
x = x * 2;
}
System.out.println(x);
Answer: B
ArithmeticException is caught, x becomes 10+5=15. Finally block executes, x becomes 15*2=30. Finally block always executes regardless of exception.
Q.196Medium
What will be the output?
try {
throw new Exception("Test");
} catch(Exception e) {
System.out.println("Caught");
} finally {
System.out.println("Finally");
}
Answer: A
The exception is caught and prints "Caught". The finally block always executes after try-catch, printing "Finally". Output: Caught Finally
Q.197Medium
What is the output of multiple catch blocks?
try {
int[] arr = {1, 2, 3};
System.out.println(arr[5]);
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Index");
} catch(Exception e) {
System.out.println("Exception");
}
Answer: A
ArrayIndexOutOfBoundsException is thrown and caught by the first catch block. The second catch block is not executed. Only the most specific matching catch block executes.
Q.198Medium
Which of the following exceptions is a checked exception?
Answer: B
FileNotFoundException is a checked exception (extends IOException). Others are unchecked - ClassCastException and NullPointerException are RuntimeExceptions, StackOverflowError is an Error.
Q.199Medium
Which statement about try-with-resources is TRUE?
Answer: C
Try-with-resources (Java 7+) automatically closes resources implementing AutoCloseable. Resources are closed in reverse order. Multiple resources can be declared with semicolon separation.
Q.200Medium
What is the output?
try {
int x = 05;
} catch(Exception e) {
System.out.println("Caught");
} catch(ArithmeticException ae) {
System.out.println("Arithmetic");
}
Answer: C
This is a compilation error. Catch blocks must be ordered from most specific to most general. ArithmeticException is more specific than Exception, so it should come first.