/Collections Framework
Concept Detail

Collections Framework

Difficulty: medium

Overview


Core interfaces: List (ordered, duplicates allowed), Set (no duplicates), Map (key-value), Queue/Deque. ArrayList = dynamic array (fast random access); LinkedList = doubly-linked (fast insert/delete, implements List + Deque). HashMap: O(1) average, default capacity 16, load factor 0.75, allows one null key. TreeMap: sorted by natural order or Comparator. ConcurrentHashMap: thread-safe, no null keys/values. Fail-fast iterators throw ConcurrentModificationException.

Practice Linked Questions


easy

Q1. Which of the following is true about ArrayList compared to a regular array in Java?


Select one answer before revealing.

medium

Q2. Which of the following is true about HashMap in Java?


Select one answer before revealing.

hard

Q3. TreeMap in Java orders its entries by:


Select one answer before revealing.

medium

Q4. What is the difference between Iterator and ListIterator in Java?


Select one answer before revealing.

easy

Q5. LinkedList in Java implements which interfaces?


Select one answer before revealing.

hard

Q6. How does ConcurrentHashMap differ from HashMap in Java?


Select one answer before revealing.

medium

Q7. Collections.sort() on a List in Java uses which underlying sort algorithm?


Select one answer before revealing.

medium

Q8. Which of the following implement the Map interface in Java? (More than one answer may be correct)


Select one answer before revealing.

hard

Q9. What are the default initial capacity and load factor of a HashMap in Java?


Select one answer before revealing.

easy

Q10. A Set in Java:


Select one answer before revealing.

hard

Q11. PriorityQueue in Java orders its elements by:


Select one answer before revealing.

hard

Q12. Which of the following are thread-safe collection classes in Java? (More than one answer may be correct)


Select one answer before revealing.

hard

Q13. A fail-fast iterator in Java:


Select one answer before revealing.

medium

Q14. What is the output of the following code? Map<String, Integer> map = new HashMap<>(); map.put("a", 1); map.put("b", 2); map.put("a", 3); System.out.println(map.size()); System.out.println(map.get("a"));


Select one answer before revealing.

hard

Q15. What happens when the following code runs? List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4)); for (Integer i : list) { if (i % 2 == 0) { list.remove(i); } }


Select one answer before revealing.

hard

Q16. What is the output of the following code? List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5)); list.remove(2); System.out.println(list);


Select one answer before revealing.

medium

Q17. What is the output of the following code? List<String> names = new ArrayList<>(Arrays.asList("Charlie", "Alice", "Bob")); Collections.sort(names); System.out.println(names.get(0));


Select one answer before revealing.

hard

Q18. What is the output of Collections.sort() on the following list? List<String> list = new ArrayList<>(Arrays.asList("Banana", "apple", "Cherry")); Collections.sort(list); System.out.println(list);


Select one answer before revealing.

medium

Q19. What is the output of the following code? List<String> list = new ArrayList<>(Arrays.asList("a", "b", "c")); Iterator<String> it = list.iterator(); while (it.hasNext()) { String s = it.next(); if (s.equals("b")) it.remove(); } System.out.println(list);


Select one answer before revealing.

easy

Q20. What is the output of the following code? Set<Integer> set = new HashSet<>(Arrays.asList(3, 1, 4, 1, 5, 9)); System.out.println(set.size()); System.out.println(set.contains(1));


Select one answer before revealing.

hard

Q21. What is the output of the following code? List<String> original = new ArrayList<>(Arrays.asList("a", "b", "c")); List<String> unmod = Collections.unmodifiableList(original); original.add("d"); System.out.println(unmod.size()); unmod.add("e");


Select one answer before revealing.

medium

Q22. What is the output of the following code? Deque<Integer> stack = new ArrayDeque<>(); stack.push(1); stack.push(2); stack.push(3); System.out.println(stack.pop()); System.out.println(stack.peek());


Select one answer before revealing.

medium

Q23. What is the output of the following code? Map<String, Integer> map = new LinkedHashMap<>(); map.put("banana", 2); map.put("apple", 1); map.put("cherry", 3); System.out.println(map.keySet());


Select one answer before revealing.

hard

Q24. What is the output of the following code? class Person implements Comparable<Person> { String name; int age; Person(String n, int a) { name=n; age=a; } public int compareTo(Person o) { return this.age - o.age; } } List<Person> p = new ArrayList<>(); p.add(new Person("Charlie", 30)); p.add(new Person("Alice", 25)); p.add(new Person("Bob", 35)); Collections.sort(p); System.out.println(p.get(0).name);


Select one answer before revealing.

easy

Q25. What is the output of the following code? List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4)); list.add(2, 99); System.out.println(list.get(2)); System.out.println(list.size());


Select one answer before revealing.

medium

Q26. What is the output of the following code? Map<String, Integer> scores = new HashMap<>(); scores.put("Alice", 85); scores.put("Bob", 90); scores.putIfAbsent("Alice", 95); System.out.println(scores.get("Alice")); System.out.println(scores.size());


Select one answer before revealing.