Java MCQ

Previous Java Multiple Choice Questions Next

Java Tricky Collections Questions

1

What is the output?
List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add(1, "C");
list.add("D");
System.out.println(list);

Correct Answer: B) [A, C, B, D]

list.add(1, "C") inserts "C" at index 1, shifting "B" to index 2. The final order is: A (0), C (1), B (2), D (3).

2

What will be printed?
Set<String> set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add(null);
set.add("Apple");
set.add(null);
System.out.println(set.size());

Correct Answer: B) 3

HashSet allows one null element and doesn't allow duplicates. Final elements: "Apple", "Banana", null.

3

What is the output?
Map<String, Integer> map = new HashMap<>();
map.put("A", 1);
map.put("B", 2);
map.put("A", 3);
map.put(null, 4);
map.put(null, 5);
System.out.println(map.size());

Correct Answer: B) 3

HashMap allows one null key and replaces duplicate keys. Final entries: "A"=3 (replaced), "B"=2, null=5 (replaced).

4

What will be printed?
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.remove(2);
System.out.println(numbers);

Correct Answer: C) UnsupportedOperationException

Arrays.asList() returns a fixed-size list backed by the array. Structural modification (remove) is not allowed.

5

What is the output?
Queue<Integer> queue = new LinkedList<>();
queue.add(1);
queue.offer(2);
queue.add(3);
System.out.print(queue.peek() + " ");
System.out.print(queue.poll() + " ");
System.out.print(queue.peek());

Correct Answer: A) 1 1 2

peek() returns head without removal, poll() removes and returns head. Queue: [1,2,3] → peek=1 → poll=1 → [2,3] → peek=2

6

What will be printed?
List<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(30);
for(Integer num : list) {
  if(num == 20) {
    list.remove(new Integer(20));
  }
}
System.out.println(list);

Correct Answer: C) ConcurrentModificationException

Modifying a collection while iterating with enhanced for-loop throws ConcurrentModificationException. Use Iterator.remove() instead.

7

What is the output?
TreeSet<Integer> set = new TreeSet<>();
set.add(5);
set.add(2);
set.add(8);
set.add(1);
System.out.println(set.first() + " " + set.last());

Correct Answer: B) 1 8

TreeSet stores elements in sorted order. first() returns smallest (1), last() returns largest (8).

8

What will be printed?
Map<String, String> map = new LinkedHashMap<>();
map.put("Z", "Last");
map.put("A", "First");
map.put("M", "Middle");
System.out.println(map.keySet());

Correct Answer: B) [Z, A, M]

LinkedHashMap maintains insertion order. Keys are returned in the order they were inserted: Z, A, M.

9

What is the output?
List<String> list1 = new ArrayList<>();
list1.add("A");
list1.add("B");
List<String> list2 = list1;
List<String> list3 = new ArrayList<>(list1);
list1.add("C");
System.out.println(list2.size() + " " + list3.size());

Correct Answer: B) 3 2

list2 references same object as list1 (size=3). list3 is a copy created before adding "C" (size=2).

10

What will be printed?
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.add(5);
pq.add(1);
pq.add(3);
pq.add(2);
while(!pq.isEmpty()) {
  System.out.print(pq.poll() + " ");
}

Correct Answer: B) 1 2 3 5

PriorityQueue returns elements in natural sorted order when polled. Smallest elements come out first.

Previous Java Multiple Choice Questions Next