Which collection would you use if you need to maintain a sorted order of elements and also need O(log n) insertion/deletion time complexity?
Answer: C
TreeSet maintains elements in sorted order using a Red-Black tree internally, providing O(log n) for add, remove, and contains operations.
Q.142Medium
Which of the following statements is true about PriorityQueue?
Answer: C
PriorityQueue is a heap-based implementation where elements are ordered based on their priority determined by natural ordering or a custom comparator provided during initialization.
Q.143Medium
What is the time complexity of get() operation on a LinkedHashMap with n elements?
Answer: A
LinkedHashMap extends HashMap and uses the same hash table for storage, maintaining O(1) average-case get operation. The doubly-linked list only maintains insertion order.
Q.144Medium
Which collection interface should be used if you need both key-value pairing and need to iterate in the order of keys' natural ordering?
Answer: B
TreeMap implements NavigableMap and SortedMap, storing key-value pairs in sorted order of keys using a Red-Black tree structure.
Q.145Medium
What will be printed? Queue<Integer> queue = new LinkedList<>(); queue.add(10); queue.add(20); queue.add(30); System.out.println(queue.poll()); System.out.println(queue.peek());
Answer: A
LinkedList implements Queue with FIFO behavior. poll() removes and returns 10. peek() returns 20 without removing it. Queue now contains [20, 30].
Advertisement
Q.146Medium
What is the difference between Thread.sleep() and Thread.yield()?
Answer: C
Thread.sleep() pauses execution for a specified milliseconds, while yield() is a hint to the scheduler that the current thread is willing to yield its turn.
Q.147Medium
What is the purpose of the synchronized keyword in Java multithreading?
Answer: B
The synchronized keyword provides mutual exclusion to ensure that only one thread can access a resource at a time, preventing race conditions.
Q.148Medium
Which of the following is NOT a thread state in Java?
Answer: D
The thread states are NEW, RUNNABLE, RUNNING, BLOCKED, WAITING, TIMED_WAITING, and TERMINATED. SLEEPING is not an official state.
Q.149Medium
What will be the output of calling start() multiple times on the same thread object?
Answer: B
Calling start() on an already started thread throws IllegalThreadStateException because a thread can only be started once.
Q.150Medium
What is a race condition in multithreading?
Answer: B
A race condition occurs when multiple threads access shared data and the final result depends on the order of execution, which is unpredictable.
Q.151Medium
Consider a synchronized method. Can multiple threads call it simultaneously on the same object?
Answer: B
A synchronized method locks the object, allowing only one thread to execute it at a time for that object instance.
Q.152Medium
What is the difference between notify() and notifyAll()?
Answer: A
notify() wakes up a single thread that is waiting on the object's monitor, while notifyAll() wakes up all threads waiting on that object.
Q.153Medium
Which class provides thread-safe operations on shared variables in Java?
Answer: C
The AtomicInteger class (and other Atomic classes) provides thread-safe operations without explicit synchronization using low-level atomic operations.
Q.154Medium
What is a daemon thread in Java?
Answer: A
A daemon thread runs in the background and doesn't prevent the JVM from exiting. When all non-daemon threads finish, the JVM exits even if daemon threads are running.
Q.155Medium
What is a deadlock in multithreading?
Answer: B
A deadlock occurs when two or more threads are blocked forever, waiting for each other to release resources they need.
Q.156Medium
What is thread pooling and which class is commonly used for it?
Answer: B
Thread pooling reuses a fixed set of threads instead of creating new ones for each task. ExecutorService provides convenient methods for thread pool management.
Q.157Medium
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();
}
}
Answer: A
First t.run() executes directly (prints T), then t.start() creates a new thread and calls run() again (prints T). So output is TT.
Q.158Medium
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?
Answer: B
When threads wait indefinitely for each other to release resources, it's a deadlock. This scenario is a classic deadlock example.
Q.159Medium
Which of the following is a thread-safe collection in Java that can be used without explicit synchronization?
Answer: C
ConcurrentHashMap is designed for concurrent access and is thread-safe. ArrayList, HashMap, and TreeMap require external synchronization for thread safety.
Q.160Medium
Which method is used to check the current state of a thread in Java?
Answer: B
The getState() method returns the Thread.State enum indicating whether a thread is NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, or TERMINATED.