Java Collections Coding Questions and Answers

Welcome to the Java Collections Framework Coding Quiz. In this quiz, we present 10 coding MCQ questions to test your coding knowledge of the Java Collections Framework. Each question has a correct and brief explanation.

1. What does the following Java code snippet output?

List<String> list = new ArrayList<>();
list.add("Java");
list.add("Python");
list.add("C++");
System.out.println(list.get(1));
a) Java
b) Python
c) C++
d) An IndexOutOfBoundsException is thrown

Answer:

b) Python

Explanation:

list.get(1) retrieves the second element in the list, which is "Python".

2. What is the result of executing this Java code snippet?

Set<Integer> set = new HashSet<>();
set.add(1);
set.add(2);
set.add(1);
System.out.println(set.size());
a) 2
b) 3
c) 4
d) Compilation error

Answer:

a) 2

Explanation:

A HashSet does not allow duplicate elements. Adding 1 twice does not change its size.

3. What will be printed by this Java code?

Map<String, Integer> map = new HashMap<>();
map.put("Alice", 30);
map.put("Bob", 25);
map.put("Alice", 35);
System.out.println(map.get("Alice"));
a) 25
b) 30
c) 35
d) null

Answer:

c) 35

Explanation:

The second put operation updates the value associated with "Alice" to 35.

4. Identify the output of the following code:

List<String> list = Arrays.asList("A", "B", "C", "D");
for (String s : list) {
    System.out.print(s + " ");
}
a) A B C D
b) A, B, C, D
c) [A, B, C, D]
d) An UnsupportedOperationException is thrown

Answer:

a) A B C D

Explanation:

The enhanced for loop iterates over each element in the list and prints it.

5. What does this code snippet output?

Queue<Integer> queue = new LinkedList<>();
queue.add(1);
queue.add(2);
queue.add(3);
System.out.println(queue.peek());
a) 1
b) 2
c) 3
d) null

Answer:

a) 1

Explanation:

peek() retrieves but does not remove the head of the queue, which is 1.

6. What is the result of executing this code?

Deque<Integer> deque = new ArrayDeque<>();
deque.offerFirst(1);
deque.offerLast(2);
System.out.println(deque.pollLast());
a) 1
b) 2
c) null
d) An exception is thrown

Answer:

b) 2

Explanation:

pollLast() retrieves and removes the last element of the deque, which is 2.

7. What will the following Java code snippet output?

Map<String, String> map = new TreeMap<>();
map.put("c", "C");
map.put("b", "B");
map.put("a", "A");
for (String key : map.keySet()) {
    System.out.print(key + " ");
}
a) a b c
b) c b a
c) A B C
d) C B A

Answer:

a) a b c

Explanation:

A TreeMap sorts its keys. The keys are iterated in ascending order.

8. What does the following code snippet print?

List<String> list = new ArrayList<>(Arrays.asList("A", "B", "C"));
list.remove("B");
System.out.println(list);
a) [A, B, C]
b) [A, C]
c) [B, C]
d) An UnsupportedOperationException is thrown

Answer:

b) [A, C]

Explanation:

The remove method removes "B" from the list, leaving "A" and "C".

9. Determine the output of this Java code:

List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
list.removeIf(n -> n % 2 == 0);
System.out.println(list);
a) [1, 2, 3, 4, 5]
b) [1, 3, 5]
c) [2, 4]
d) []

Answer:

b) [1, 3, 5]

Explanation:

removeIf removes elements that match the given predicate, which in this case are the even numbers.

10. What is the result of the following code snippet?

Set<String> set = new LinkedHashSet<>(Arrays.asList("A", "B", "C"));
set.add("D");
set.add("B");
System.out.println(set);
a) [A, B, C, D]
b) [A, C, D, B]
c) [D, A, B, C]
d) [B, A, C, D]

Answer:

a) [A, B, C, D]

Explanation:

A LinkedHashSet maintains insertion order and does not allow duplicates. "B" is not added again.

11. What will this Java code snippet output?

Map<Integer, String> map = new HashMap<>();
map.put(1, "A");
map.put(2, "B");
map.put(3, "C");
map.replace(2, "D");
System.out.println(map);
a) {1=A, 2=B, 3=C}
b) {1=A, 2=D, 3=C}
c) {1=A, 3=C}
d) {1=A, 2=B, 3=C, 2=D}

Answer:

b) {1=A, 2=D, 3=C}

Explanation:

The replace method updates the value associated with key 2 to "D".

12. Identify the output of this code:

List<Integer> list = new LinkedList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(1, 4);
System.out.println(list);
a) [1, 4, 2, 3]
b) [4, 1, 2, 3]
c) [1, 2, 3, 4]
d) [1, 2, 4, 3]

Answer:

a) [1, 4, 2, 3]

Explanation:

The add method with an index adds the element at the specified position, shifting others to the right.

13. What does this Java code snippet output?

Queue<String> queue = new PriorityQueue<>();
queue.offer("C");
queue.offer("A");
queue.offer("B");
System.out.println(queue.poll());
a) A
b) B
c) C
d) null

Answer:

a) A

Explanation:

A PriorityQueue orders elements according to their natural ordering. "A" is polled first as it's the smallest.

14. What is the output of the following Java code?

List<Integer> list = new ArrayList<>();
for (int i = 1; i <= 5; i++) {
    list.add(i);
}
list.set(2, 10);
System.out.println(list);
a) [1, 2, 10, 4, 5]
b) [1, 10, 3, 4, 5]
c) [1, 2, 3, 10, 5]
d) [1, 2, 3, 4, 5]

Answer:

a) [1, 2, 10, 4, 5]

Explanation:

The set method replaces the element at the specified index. Index 2 (third element) is changed from 3 to 10.

15. What will the following Java code snippet output?

Map<Integer, String> map = new LinkedHashMap<>();
map.put(3, "C");
map.put(1, "A");
map.put(2, "B");
for (Map.Entry<Integer, String> entry : map.entrySet()) {
    System.out.print(entry.getValue() + " ");
}
a) A B C
b) C A B
c) B C A
d) C B A

Answer:

b) C A B

Explanation:

A LinkedHashMap maintains insertion order. The values are iterated in the order they were put into the map.

16. Identify the output of this code:

List<Integer> list = new CopyOnWriteArrayList<>(Arrays.asList(1, 2, 3));
for (Integer item : list) {
    if (item == 2) {
        list.remove(item);
    }
}
System.out.println(list);
a) [1, 2, 3]
b) [1, 3]
c) [2, 3]
d) [1, 2]

Answer:

b) [1, 3]

Explanation:

CopyOnWriteArrayList allows safe removal during iteration. The element 2 is removed, leaving [1, 3].

17. What does this Java code snippet output?

Deque<Integer> deque = new ArrayDeque<>();
deque.addFirst(1);
deque.addFirst(2);
deque.addLast(3);
System.out.println(deque);
a) [1, 2, 3]
b) [2, 1, 3]
c) [3, 2, 1]
d) [3, 1, 2]

Answer:

b) [2, 1, 3]

Explanation:

Elements are added to the front and back of the deque, resulting in [2, 1, 3].

18. What is the result of the following code snippet?

List<String> list = new Vector<>();
list.add("A");
list.add("B");
list.add("C");
System.out.println(list.contains("B"));
a) true
b) false
c) Compilation error
d) Runtime error

Answer:

a) true

Explanation:

The contains method checks if the list contains the specified element. Since "B" is in the list, it returns true.

19. What will the following Java code snippet output?

SortedSet<String> sortedSet = new TreeSet<>();
sortedSet.add("C");
sortedSet.add("A");
sortedSet.add("B");
System.out.println(sortedSet.first());
a) A
b) B
c) C
d) null

Answer:

a) A

Explanation:

A TreeSet sorts its elements. The first method returns the first (lowest) element, which is "A".

20. Identify the output of this code:

Map<Integer, String> map = new HashMap<>();
map.put(1, "One");
map.put(2, "Two");
map.put(3, "Three");
System.out.println(map.containsKey(2));
a) true
b) false
c) null
d) Compilation error

Answer:

a) true

Explanation:

containsKey checks whether the map contains a mapping for the specified key. Since there is a key 2, it returns true.

21. What does this Java code snippet output?

Queue<Integer> queue = new PriorityQueue<>(Comparator.reverseOrder());
queue.offer(3);
queue.offer(1);
queue.offer(2);
System.out.println(queue.poll());
a) 1
b) 2
c) 3
d) null

Answer:

c) 3

Explanation:

The PriorityQueue is initialized with a Comparator for reverse ordering, so it polls the largest element first, which is 3.

22. What is the result of executing this code?

List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3));
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()) {
    if (iterator.next() == 2) {
        iterator.remove();
    }
}
System.out.println(list);
a) [1, 2, 3]
b) [1, 3]
c) [2, 3]
d) [1, 2]

Answer:

b) [1, 3]

Explanation:

The Iterator removes the element 2 from the list, leaving [1, 3].

23. What will the following Java code snippet output?

Set<String> set = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
set.add("apple");
set.add("Banana");
set.add("APPLE");
System.out.println(set);
a) [APPLE, Banana, apple]
b) [apple, Banana]
c) [Banana, APPLE, apple]
d) [apple, APPLE, Banana]

Answer:

b) [apple, Banana]

Explanation:

The TreeSet is initialized with a case-insensitive order. It considers "apple" and "APPLE" as duplicates.

24. Identify the output of this code:

Map<Integer, String> map = new ConcurrentHashMap<>();
map.put(1, "A");
map.put(2, "B");
map.remove(1);
System.out.println(map);
a) {1=A, 2=B}
b) {2=B}
c) {1=A}
d) {}

Answer:

b) {2=B}

Explanation:

The remove method removes the mapping for key 1, leaving {2=B}.

25. What does this Java code snippet output?

List<String> list = new LinkedList<>();
list.add("A");
list.add("B");
list.addFirst("C");
System.out.println(list);
a) [A, B, C]
b) [C, A, B]
c) [B, A, C]
d) [B, C, A]

Answer:

b) [C, A, B]

Explanation:

The addFirst method adds the element at the beginning of the list, resulting in [C, A, B].

26. What is the result of the following code snippet?

Deque<String> deque = new LinkedList<>();
deque.offer("A");
deque.offerFirst("B");
deque.offerLast("C");
System.out.println(deque.poll());
a) A
b) B
c) C
d) null

Answer:

b) B

Explanation:

offerFirst adds "B" at the beginning. poll retrieves and removes the first element, which is "B".

Comments