Which interface does HashMap implement in Java Collections Framework?
Answer: A
HashMap implements the Map interface which stores key-value pairs. It does not implement Collection, List, or Set interfaces directly.
Q.2Easy
What is the time complexity of get() method in HashMap?
Answer: C
HashMap provides O(1) average time complexity for get() operation. In worst case with hash collisions, it can be O(n).
Q.3Easy
Which of the following collections maintains insertion order?
Answer: C
LinkedHashSet maintains insertion order using doubly-linked list. HashSet doesn't maintain order, TreeSet maintains sorted order, HashMap doesn't maintain insertion order.
Q.4Easy
What is the difference between ArrayList and LinkedList?
Answer: A
ArrayList is backed by a resizable array providing O(1) random access. LinkedList uses doubly-linked list providing O(1) insertion/deletion at ends but O(n) for random access.
Q.5Easy
Which collection does NOT allow duplicate elements?
Answer: B
Set interface implementations like HashSet do not allow duplicates. Lists (ArrayList, LinkedList, Vector) allow duplicates.
Advertisement
Q.6Easy
What is the difference between TreeSet and HashSet in sorting?
Answer: A
TreeSet maintains elements in sorted order using Red-Black tree. HashSet is unordered and uses hash table for storage.
Q.7Easy
Which method in Set collection prevents duplicate insertion by returning false?
Answer: A
The add() method in Set returns boolean - true if element is added, false if duplicate exists.
Q.8Easy
What is the output of LinkedHashMap iteration in the following code?
LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
map.put("A", 1);
map.put("B", 2);
map.put("C", 3);
for(String key : map.keySet()) System.out.print(key);
Answer: A
LinkedHashMap maintains insertion order. Elements are iterated in the order they were inserted.
Q.9Easy
Which collection class implements NavigableMap interface?
Answer: B
TreeMap implements NavigableMap which provides methods like higherKey(), lowerKey(), floorEntry(), etc.
Q.10Easy
In the context of streams and collections, which method returns a Sequential Stream in Java 8+?
Answer: B
collection.stream() returns a sequential stream, while parallelStream() returns a parallel stream.
Q.11Easy
Which Collection method was introduced in Java 9 to create immutable collections?
Answer: B
Java 9 introduced factory methods like List.of(), Set.of(), Map.of() for creating immutable collections.
Q.12Easy
Which of the following Collection interfaces does NOT support duplicate elements?
Answer: A
Set interface does not allow duplicate elements, while List, Queue, and Collection support duplicates.
Q.13Easy
What is the default initial capacity of a HashMap in Java?
Answer: B
HashMap has a default initial capacity of 16 and a load factor of 0.75.
Q.14Easy
Which collection class is synchronized by default?
Answer: C
Hashtable is a legacy synchronized collection class. HashMap, ArrayList, and TreeSet are not synchronized by default.
Q.15Easy
What does the poll() method return when called on an empty Queue?
Answer: B
poll() returns null if the queue is empty, while remove() throws NoSuchElementException.
Q.16Easy
Which of the following correctly represents the hierarchy of Collection framework?
Answer: B
The correct hierarchy is Iterable (interface) -> Collection -> List/Set/Queue/Deque interfaces.
Q.17Easy
Which interface in Java Collections Framework does not allow duplicate elements?
Answer: B
Set interface explicitly does not allow duplicate elements. List allows duplicates, Queue is for ordered collections, and Map stores key-value pairs.
Q.18Easy
What is the underlying data structure of HashMap in Java?
Answer: B
HashMap uses a hash table with buckets (arrays of linked lists or red-black trees) to store key-value pairs for efficient O(1) average lookup time.
Q.19Easy
Which of the following maintains insertion order in Java?
Answer: C
LinkedHashSet maintains insertion order using a doubly-linked list. HashSet does not maintain order, TreeSet maintains sorted order, and ConcurrentHashMap doesn't guarantee insertion order.
Q.20Easy
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.