Java Collections Framework (List, Set, Map) Quiz
35 in-depth questions (18 with code) covering List types (ArrayList, LinkedList), Set types (HashSet, TreeSet), Map types (HashMap, TreeMap), Collections utility class, and choosing the right collection for different use cases in Java.
Question 1
What is the main difference between ArrayList and LinkedList?
Question 2
When should you use ArrayList over LinkedList?
Question 3
What is the time complexity of ArrayList.get(index)?
Question 4
What is the time complexity of LinkedList.add(index, element)?
Question 5
Which Set implementation maintains insertion order?
Question 6
Which Set implementation keeps elements sorted?
Question 7
What is the output of this HashSet example?
Set<String> set = new HashSet<>();
set.add("apple");
set.add("banana");
set.add("apple"); // duplicate
System.out.println(set.size());Question 8
Which Map implementation maintains insertion order?
Question 9
Which Map implementation keeps keys sorted?
Question 10
What is the time complexity of HashMap.get(key)?
Question 11
What is the result of this ArrayList capacity example?
List<String> list = new ArrayList<>(10);
list.add("item1");
list.add("item2");
System.out.println("Size: " + list.size() + ", but capacity is internally managed");Question 12
How does Collections.sort() work internally?
Question 13
What is the output of this Collections utility example?
List<String> list = Arrays.asList("banana", "apple", "cherry");
Collections.sort(list);
System.out.println(list.get(0));Question 14
When should you use TreeSet over HashSet?
Question 15
What is the difference between HashMap and Hashtable?
Question 16
What is the result of this LinkedList vs ArrayList performance?
List<String> arrayList = new ArrayList<>();
List<String> linkedList = new LinkedList<>();
// Adding to end
arrayList.add("item"); // O(1)
linkedList.add("item"); // O(1)
// But for middle insertions:
arrayList.add(0, "first"); // O(n)
linkedList.add(0, "first"); // O(1)Question 17
How do you create an immutable List?
List<String> mutable = new ArrayList<>();
mutable.add("item1");
mutable.add("item2");
List<String> immutable = Collections.unmodifiableList(mutable);Question 18
What is the output of this TreeSet example?
Set<Integer> set = new TreeSet<>();
set.add(3);
set.add(1);
set.add(2);
System.out.println(set.toString());Question 19
When should you use LinkedHashMap?
Question 20
What is the time complexity of TreeMap.get(key)?
Question 21
How do you find the maximum element in a Collection?
List<Integer> numbers = Arrays.asList(3, 1, 4, 1, 5);
Integer max = Collections.max(numbers);
System.out.println(max);Question 22
What is the difference between List and Set?
Question 23
What is the result of this HashMap null key example?
Map<String, String> map = new HashMap<>();
map.put(null, "nullValue");
map.put("key", "value");
System.out.println(map.get(null));Question 24
How do you create a synchronized collection?
List<String> list = new ArrayList<>();
List<String> syncList = Collections.synchronizedList(list);Question 25
When should you use ArrayList over LinkedList for large lists?
Question 26
What is the output of this LinkedHashSet example?
Set<String> set = new LinkedHashSet<>();
set.add("banana");
set.add("apple");
set.add("cherry");
set.add("apple"); // duplicate
System.out.println(set.toString());Question 27
How do you reverse a List?
List<String> list = Arrays.asList("a", "b", "c");
Collections.reverse(list);
System.out.println(list);Question 28
What is the difference between Map and Set?
Question 29
What is the result of this TreeMap example?
Map<String, Integer> map = new TreeMap<>();
map.put("banana", 2);
map.put("apple", 1);
map.put("cherry", 3);
System.out.println(map.keySet());Question 30
How do you check if a Collection is empty?
List<String> list = new ArrayList<>();
if (list.isEmpty()) {
System.out.println("Empty");
}Question 31
When should you use HashSet over TreeSet?
Question 32
What is the output of this Collections.binarySearch example?
List<String> list = Arrays.asList("apple", "banana", "cherry");
int index = Collections.binarySearch(list, "banana");
System.out.println(index);Question 33
How do you create a Set from a List while removing duplicates?
List<String> list = Arrays.asList("a", "b", "a", "c");
Set<String> set = new HashSet<>(list);
System.out.println(set.size());Question 34
What is the time complexity of LinkedList.get(index)?
Question 35
Mastering Java Collections means you now write code that:
