What is the primary purpose of the volatile keyword in Java multithreading?
Answer: B
The volatile keyword ensures that any read of a volatile variable will read the most recent write by any thread. It provides visibility guarantees but not atomicity, making it lighter than synchronization for simple flag checks.
Q.102Easy
Which of the following will cause a deadlock situation?
Answer: A
Deadlock occurs when two or more threads are waiting indefinitely for locks held by each other. Option B creates a reentrant situation (allowed in ReentrantLock). Option C is problematic but not necessarily deadlock. Option D is safe if used properly.
Q.103Easy
Which collection class is thread-safe without explicit synchronization?
Answer: C
ConcurrentHashMap uses segment-based locking (in older versions) or fine-grained locking to provide thread-safety without synchronizing the entire map. Other options require external synchronization or Collections.synchronizedMap().
Q.104Easy
Which method is used to prevent race conditions by allowing only one thread to access a resource at a time?
Answer: A
The synchronized keyword creates a critical section that only one thread can access at a time, preventing race conditions. volatile ensures visibility but not atomicity.
Q.105Easy
Which of the following collections is thread-safe in Java?
Answer: A
ConcurrentHashMap is designed for concurrent access without requiring external synchronization. HashMap and others are not thread-safe.
Advertisement
Q.106Easy
What is the output of the following code?
Thread t = new Thread(() -> System.out.println(Thread.currentThread().getName()));
t.start();
Answer: A
When a new Thread is created without a name parameter, it gets a default name 'Thread-n' where n is a counter. The thread will print 'Thread-0' as it is the first thread created.
Q.107Easy
Which method must be implemented to create a thread in Java?
Answer: B
The run() method must be implemented when creating a thread either by extending Thread class or implementing Runnable interface. The start() method calls run() internally.
Q.108Easy
What is the difference between start() and run() methods in threading?
Answer: A
start() creates a new thread and calls run() in that new thread, while directly calling run() executes it in the current thread without creating a new thread.
Q.109Easy
Which of the following thread states is NOT a valid Java thread state?
Answer: C
Java thread states are: NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, and TERMINATED. SUSPENDED is not a valid state in Java's threading model.
Q.110Easy
Which of the following methods will cause a thread to release all locks it holds while waiting?
Answer: A
The wait() method causes a thread to release all acquired locks and enter a waiting state until notify() or notifyAll() is called. sleep(), yield(), and join() do not release locks.
Q.111Easy
A developer needs to ensure that exactly 5 threads complete their tasks before proceeding to the next phase. Which synchronization utility is most appropriate?
Answer: B
CountDownLatch is designed for one-time barrier scenarios where threads wait for a countdown to reach zero. With an initial count of 5, it perfectly suits this use case. Semaphore is reusable, Phaser handles multiple phases, and Mutex is not a Java class.
Q.112Easy
Which of the following is a checked exception in Java?
Answer: B
IOException is a checked exception that must be caught or declared in the method signature. NullPointerException, ArrayIndexOutOfBoundsException, and ArithmeticException are unchecked exceptions.
Q.113Easy
Which exception is thrown when a method receives an illegal or inappropriate argument?
Answer: B
IllegalArgumentException is thrown to indicate that a method has been passed an illegal or inappropriate argument. It is an unchecked exception.
Q.114Easy
Can you have a try block without a catch block in Java?
Answer: B
A try block must be followed by either a catch block or a finally block (or both). A try block alone is not valid syntax.
Q.115Easy
Which of the following is NOT a subclass of Throwable in Java?
Answer: D
Runnable is a functional interface, not related to exception hierarchy. Error and Exception are direct subclasses of Throwable. RuntimeException is a subclass of Exception.
Q.116Easy
Which exception is thrown when trying to access a method of a null object?
Answer: B
NullPointerException is thrown when attempting to call a method or access a property on a null object reference in Java.
Q.117Easy
Which exception is thrown when a string cannot be converted to a number?
Answer: A
NumberFormatException is thrown when attempting to convert a string to a numeric type but the string does not have an appropriate format. It's an unchecked exception.
Q.118Easy
What is the output of this code?
public class Test {
public static void main(String[] args) {
try {
int[] arr = {1, 2};
System.out.println(arr[5]);
} catch (Exception e) {
System.out.println("Caught");
}
}
}
Answer: A
ArrayIndexOutOfBoundsException is caught by the generic Exception catch block, printing 'Caught'.
Q.119Easy
In Java exception handling, what is the order of execution in try-finally-catch block?
Answer: A
The correct order is try block executes first, then catch block (if exception occurs), and finally block always executes at the end.
Q.120Easy
Which exception is thrown when a numeric string cannot be converted to a number?
Answer: A
NumberFormatException is thrown by Integer.parseInt(), Double.parseDouble() when string format is invalid.