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));
Answer:
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());
Answer:
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"));
Answer:
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 + " ");
}
Answer:
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());
Answer:
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());
Answer:
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 + " ");
}
Answer:
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);
Answer:
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);
Answer:
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);
Answer:
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);
Answer:
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);
Answer:
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());
Answer:
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);
Answer:
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() + " ");
}
Answer:
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);
Answer:
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);
Answer:
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"));
Answer:
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());
Answer:
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));
Answer:
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());
Answer:
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);
Answer:
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);
Answer:
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);
Answer:
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);
Answer:
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());
Answer:
Explanation:
offerFirst adds "B" at the beginning. poll retrieves and removes the first element, which is "B".
Comments
Post a Comment
Leave Comment