What is the primary purpose of the Comparator interface in Collections Framework?
Answer: B
Comparator interface allows defining custom comparison logic for sorting. It provides compare(T o1, T o2) method to establish custom ordering independent of the object's natural order.
Q.82Easy
Which interface in Java Collections Framework is used to maintain insertion order while allowing duplicates?
Answer: A
List interface maintains insertion order and allows duplicate elements. ArrayList, LinkedList, and Vector are common implementations.
Q.83Easy
What is the time complexity of get() operation in HashMap in average case?
Answer: C
HashMap provides O(1) average case time complexity for get, put, and remove operations due to hash-based indexing.
Q.84Easy
Which collection class is synchronized and thread-safe by default?
Answer: B
Hashtable is a legacy synchronized collection class. For modern applications, ConcurrentHashMap is preferred over Hashtable.
Q.85Easy
What does the PriorityQueue in Java Collections Framework guarantee?
Answer: C
PriorityQueue is a min-heap by default where elements are retrieved based on priority, not insertion order.
Advertisement
Q.86Easy
Consider: List<String> list = new ArrayList<>(); list.add("Java"); list.add("Python"); What will list.get(1) return?
Answer: B
ArrayList uses 0-based indexing. Index 1 refers to the second element added, which is "Python".
Q.87Easy
Which method returns true if the map contains the specified value?
Answer: B
The containsValue() method checks if the map contains a specified value. containsKey() checks for keys.
Q.88Easy
Which of the following collections maintains insertion order while allowing fast random access?
Answer: A
ArrayList maintains insertion order and provides O(1) random access via index. LinkedHashMap maintains insertion order but is a Map, not a List.
Q.89Easy
What will be the output of the following code? Set<Integer> set = new TreeSet<>(); set.add(5); set.add(2); set.add(8); set.add(2); System.out.println(set.size());
Answer: A
TreeSet does not allow duplicates. Adding 2 twice means it is stored only once. Final set contains {2, 5, 8}, so size is 3.
Q.90Easy
What will happen when you execute: Map<String, String> map = new HashMap<>(); map.put("key", null); System.out.println(map.get("key"));
Answer: B
HashMap allows null values. The code stores null as the value for key 'key' and retrieves it successfully, printing null.
Q.91Easy
Which interface must be implemented to create a thread in Java?
Answer: A
The Runnable interface is the correct interface to implement for creating threads in Java. It has a single abstract method run().
Q.92Easy
Which method is used to stop a thread in Java?
Answer: B
The interrupt() method is the proper way to stop a thread in Java. The stop() method is deprecated and dangerous.
Q.93Easy
Which method is called to make a thread wait until another thread completes?
Answer: B
The join() method causes the current thread to wait until the thread on which it is called completes its execution.
Q.94Easy
Which of the following is a valid way to create a thread in Java?
Answer: C
Both extending Thread class and implementing Runnable interface are valid approaches to create threads in Java.
Q.95Easy
In Java multithreading, which method is used to pause the execution of a thread for a specified number of milliseconds without releasing locks?
Answer: A
Thread.sleep() pauses thread execution for specified milliseconds while retaining all locks. pause(), halt(), and suspend() are not standard Java methods for this purpose.
Q.96Easy
Which of the following statements about the notify() method is correct?
Answer: B
notify() wakes up a single arbitrary thread that is waiting on the same monitor. notifyAll() wakes up all waiting threads.
Q.97Easy
Which exception is thrown when a thread is interrupted while waiting?
Answer: B
InterruptedException is thrown when a thread is interrupted during sleep(), wait(), or join() operations.
Q.98Easy
What is the return type of the join() method in Java threading?
Answer: C
The join() method returns void. It causes the calling thread to wait until the thread on which it is called completes its execution.
Q.99Easy
Which of the following best describes the purpose of the Runnable interface in Java?
Answer: B
The Runnable interface contains a single run() method that defines the code to be executed when a thread runs. It's preferred over extending Thread class.
Q.100Easy
Which of the following statements about Java threads is correct?
Answer: C
A thread can only exist in one state at a time (NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, or TERMINATED). Calling start() multiple times throws IllegalThreadStateException. The start() method should be called, not run() directly.