Which method is used to remove an element from ArrayList while iterating?
Answer: B
Using remove() directly in loop causes ConcurrentModificationException. Iterator.remove() is the safe way to remove elements while iterating.
Q.2Medium
What is the load factor in HashMap? What is its default value?
Answer: B
Load factor is the ratio of size to capacity. Default load factor in HashMap is 0.75, which provides good balance between time and space complexity.
Q.3Medium
What is the time complexity of add() operation in TreeSet?
Answer: B
TreeSet is backed by a TreeMap which uses Red-Black tree. Add operation requires O(log n) time for tree balancing.
Q.4Medium
Which of the following is thread-safe?
Answer: C
ConcurrentHashMap is thread-safe and uses bucket-level locking. HashMap, TreeMap, and LinkedHashMap are not thread-safe.
Q.5Medium
What does the Comparable interface do in Collections Framework?
Answer: A
Comparable interface provides compareTo() method to define natural ordering for objects. Used by TreeSet and TreeMap for sorting.
Advertisement
Q.6Medium
Which collection is best for frequent insertions and deletions in the middle?
Answer: B
LinkedList provides O(1) insertion/deletion at ends and O(n) at middle. ArrayList requires O(n) for middle insertions/deletions due to shifting.
Q.7Medium
Given:
Map<String, Integer> map = new HashMap<>();
map.put("A", 10);
map.put("B", 20);
map.put("A", 30);
What will be the size of map?
Answer: B
HashMap stores unique keys. Putting same key 'A' again replaces the old value. Size remains 2 with keys A and B.
Q.8Medium
Which interface provides sorted ordering in Collections Framework?
Answer: B
SortedSet and SortedMap interfaces provide sorted ordering. TreeSet implements SortedSet, TreeMap implements SortedMap.
Q.9Medium
Which collection class is synchronized and legacy?
Answer: C
Vector is a legacy synchronized collection similar to ArrayList. It is thread-safe but slower. ArrayList is preferred with Collections.synchronizedList() for synchronization.
Q.10Medium
Which of the following statements about Queue interface is correct?
Answer: C
offer() returns false if element cannot be added, while add() throws exception. poll() returns null if empty.
Q.11Medium
What will be the result of executing this code?
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.add(5);
pq.add(3);
pq.add(7);
System.out.println(pq.peek());
Answer: B
PriorityQueue orders elements based on their natural ordering (min-heap by default). peek() returns 3, the minimum element.
Q.12Medium
Consider the following code. What is the behavior?
Collection<String> col = new ArrayList<>();
col.add("Java");
Iterator<String> it = col.iterator();
while(it.hasNext()) {
String s = it.next();
col.remove(s);
}
Answer: B
Modifying collection directly while iterating throws ConcurrentModificationException. Use iterator.remove() instead.
Q.13Medium
What is the output of the following code?
List<String> list = Arrays.asList("A", "B", "C");
list.add("D");
System.out.println(list.size());
Answer: C
Arrays.asList() returns a fixed-size list backed by array. add() operation is not supported.
Q.14Medium
What is the time complexity of contains() operation in HashSet?
Answer: A
HashSet uses hash table internally. Average case is O(1), worst case O(n) when hash collisions occur.
Q.15Medium
Which of the following is a fail-fast iterator behavior?
Answer: B
Fail-fast iterators throw ConcurrentModificationException when collection is modified during iteration.
Q.16Medium
Consider a scenario where you need fast random access and frequent insertions in the middle. Which collection is most suitable?
Answer: B
LinkedList provides O(1) insertions/deletions at any position. ArrayList is O(n) for middle insertions due to shifting.
Q.17Medium
What does the removeIf() method in Collection interface do?
Answer: B
removeIf(Predicate) removes all elements that satisfy the given predicate condition (Java 8+).
Q.18Medium
What is the time complexity of add() operation in ArrayList?
Answer: A
ArrayList.add() has O(1) amortized time complexity. When capacity is exceeded, resizing takes O(n).
Q.19Medium
Which collection maintains insertion order and also provides thread-safety?
Answer: D
CopyOnWriteArrayList maintains insertion order and is thread-safe. It creates a copy of the array on modification.
Q.20Medium
What is the difference between Iterator and ListIterator?
Answer: A
ListIterator extends Iterator and provides bidirectional traversal using previous() and next() methods.