Java Collections Framework (List, Set, Map) Quiz

Java
0 Passed
0% acceptance

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.

35 Questions
~70 minutes
1

Question 1

What is the main difference between ArrayList and LinkedList?

A
ArrayList is better for random access, LinkedList is better for insertions/deletions
B
ArrayList uses more memory
C
LinkedList is synchronized
D
They are identical in performance
2

Question 2

When should you use ArrayList over LinkedList?

A
When you need fast random access and few insertions/deletions in the middle
B
When you do many insertions/deletions at the beginning
C
When memory is very limited
D
When you need thread safety
3

Question 3

What is the time complexity of ArrayList.get(index)?

A
O(1)
B
O(n)
C
O(log n)
D
O(n²)
4

Question 4

What is the time complexity of LinkedList.add(index, element)?

A
O(n)
B
O(1)
C
O(log n)
D
O(n²)
5

Question 5

Which Set implementation maintains insertion order?

A
LinkedHashSet
B
HashSet
C
TreeSet
D
EnumSet
6

Question 6

Which Set implementation keeps elements sorted?

A
TreeSet
B
HashSet
C
LinkedHashSet
D
CopyOnWriteArraySet
7

Question 7

What is the output of this HashSet example?

java
Set<String> set = new HashSet<>();
set.add("apple");
set.add("banana");
set.add("apple");  // duplicate
System.out.println(set.size());
A
2
B
3
C
Compilation error
D
Runtime exception
8

Question 8

Which Map implementation maintains insertion order?

A
LinkedHashMap
B
HashMap
C
TreeMap
D
ConcurrentHashMap
9

Question 9

Which Map implementation keeps keys sorted?

A
TreeMap
B
HashMap
C
LinkedHashMap
D
IdentityHashMap
10

Question 10

What is the time complexity of HashMap.get(key)?

A
O(1) average case
B
O(n)
C
O(log n)
D
O(1) worst case
11

Question 11

What is the result of this ArrayList capacity example?

java
List<String> list = new ArrayList<>(10);
list.add("item1");
list.add("item2");
System.out.println("Size: " + list.size() + ", but capacity is internally managed");
A
Size: 2, but capacity is internally managed
B
Size: 10
C
Compilation error
D
Runtime exception
12

Question 12

How does Collections.sort() work internally?

A
Uses a tuned quicksort algorithm for primitives, mergesort for objects
B
Always uses bubble sort
C
Requires elements to be sorted manually
D
Only works on arrays
13

Question 13

What is the output of this Collections utility example?

java
List<String> list = Arrays.asList("banana", "apple", "cherry");
Collections.sort(list);
System.out.println(list.get(0));
A
apple
B
banana
C
cherry
D
Compilation error
14

Question 14

When should you use TreeSet over HashSet?

A
When you need elements to be maintained in sorted order
B
When you need fast lookup
C
When order doesn't matter
D
When memory is limited
15

Question 15

What is the difference between HashMap and Hashtable?

A
HashMap is not synchronized, Hashtable is synchronized
B
HashMap allows null keys, Hashtable doesn't
C
HashMap is faster
D
All of the above
16

Question 16

What is the result of this LinkedList vs ArrayList performance?

java
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)
A
LinkedList is better for frequent middle insertions
B
ArrayList is always better
C
They perform identically
D
LinkedList uses more memory
17

Question 17

How do you create an immutable List?

java
List<String> mutable = new ArrayList<>();
mutable.add("item1");
mutable.add("item2");
List<String> immutable = Collections.unmodifiableList(mutable);
A
Use Collections.unmodifiableList()
B
Use Arrays.asList()
C
Use List.of() in Java 9+
D
All of the above
18

Question 18

What is the output of this TreeSet example?

java
Set<Integer> set = new TreeSet<>();
set.add(3);
set.add(1);
set.add(2);
System.out.println(set.toString());
A
[1, 2, 3]
B
[3, 1, 2]
C
Unpredictable order
D
Compilation error
19

Question 19

When should you use LinkedHashMap?

A
When you need predictable iteration order (insertion order)
B
When you need sorted keys
C
When memory is critical
D
When you need thread safety
20

Question 20

What is the time complexity of TreeMap.get(key)?

A
O(log n)
B
O(1)
C
O(n)
D
O(n log n)
21

Question 21

How do you find the maximum element in a Collection?

java
List<Integer> numbers = Arrays.asList(3, 1, 4, 1, 5);
Integer max = Collections.max(numbers);
System.out.println(max);
A
Collections.max(collection)
B
Collections.findMax(collection)
C
collection.getMax()
D
Manually iterate
22

Question 22

What is the difference between List and Set?

A
List allows duplicates and maintains order, Set doesn't allow duplicates
B
List is faster
C
Set maintains order
D
They are identical
23

Question 23

What is the result of this HashMap null key example?

java
Map<String, String> map = new HashMap<>();
map.put(null, "nullValue");
map.put("key", "value");
System.out.println(map.get(null));
A
nullValue
B
null
C
Compilation error
D
Runtime exception
24

Question 24

How do you create a synchronized collection?

java
List<String> list = new ArrayList<>();
List<String> syncList = Collections.synchronizedList(list);
A
Collections.synchronizedList()
B
Use Vector or Hashtable
C
Use ConcurrentHashMap
D
All of the above
25

Question 25

When should you use ArrayList over LinkedList for large lists?

A
Almost always, due to better cache locality and memory usage
B
When you do many insertions at the beginning
C
When memory is very limited
D
When you need thread safety
26

Question 26

What is the output of this LinkedHashSet example?

java
Set<String> set = new LinkedHashSet<>();
set.add("banana");
set.add("apple");
set.add("cherry");
set.add("apple");  // duplicate
System.out.println(set.toString());
A
[banana, apple, cherry]
B
[apple, banana, cherry]
C
Unpredictable order
D
Compilation error
27

Question 27

How do you reverse a List?

java
List<String> list = Arrays.asList("a", "b", "c");
Collections.reverse(list);
System.out.println(list);
A
Collections.reverse(list)
B
list.reverse()
C
Manually swap elements
D
Use a loop
28

Question 28

What is the difference between Map and Set?

A
Map stores key-value pairs, Set stores unique values
B
Map allows duplicates
C
Set is faster
D
They are identical
29

Question 29

What is the result of this TreeMap example?

java
Map<String, Integer> map = new TreeMap<>();
map.put("banana", 2);
map.put("apple", 1);
map.put("cherry", 3);
System.out.println(map.keySet());
A
[apple, banana, cherry]
B
[banana, apple, cherry]
C
Unpredictable order
D
Compilation error
30

Question 30

How do you check if a Collection is empty?

java
List<String> list = new ArrayList<>();
if (list.isEmpty()) {
    System.out.println("Empty");
}
A
collection.isEmpty()
B
collection.size() == 0
C
Both are equivalent
D
collection == null
31

Question 31

When should you use HashSet over TreeSet?

A
When you don't need sorted order and want better performance
B
When you need sorted elements
C
When memory is limited
D
When you need insertion order
32

Question 32

What is the output of this Collections.binarySearch example?

java
List<String> list = Arrays.asList("apple", "banana", "cherry");
int index = Collections.binarySearch(list, "banana");
System.out.println(index);
A
1
B
-1
C
0
D
Compilation error
33

Question 33

How do you create a Set from a List while removing duplicates?

java
List<String> list = Arrays.asList("a", "b", "a", "c");
Set<String> set = new HashSet<>(list);
System.out.println(set.size());
A
new HashSet<>(list)
B
Collections.toSet(list)
C
list.toSet()
D
Manually iterate and add
34

Question 34

What is the time complexity of LinkedList.get(index)?

A
O(n)
B
O(1)
C
O(log n)
D
O(n²)
35

Question 35

Mastering Java Collections means you now write code that:

A
Selects the optimal collection type based on access patterns, performance requirements, and memory constraints, resulting in efficient, scalable applications
B
Always uses ArrayList
C
Always uses HashMap
D
Ignores performance considerations

QUIZZES IN Java