Java MCQ
Java Tricky Collections Questions
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);
list.add("A");
list.add("B");
list.add(1, "C");
list.add("D");
System.out.println(list);
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).
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());
set.add("Apple");
set.add("Banana");
set.add(null);
set.add("Apple");
set.add(null);
System.out.println(set.size());
HashSet allows one null element and doesn't allow duplicates. Final elements: "Apple", "Banana", null.
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());
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());
HashMap allows one null key and replaces duplicate keys. Final entries: "A"=3 (replaced), "B"=2, null=5 (replaced).
What will be printed?
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.remove(2);
System.out.println(numbers);
numbers.remove(2);
System.out.println(numbers);
Arrays.asList() returns a fixed-size list backed by the array. Structural modification (remove) is not allowed.
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());
queue.add(1);
queue.offer(2);
queue.add(3);
System.out.print(queue.peek() + " ");
System.out.print(queue.poll() + " ");
System.out.print(queue.peek());
peek() returns head without removal, poll() removes and returns head. Queue: [1,2,3] → peek=1 → poll=1 → [2,3] → peek=2
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);
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);
Modifying a collection while iterating with enhanced for-loop throws ConcurrentModificationException. Use Iterator.remove() instead.
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());
set.add(5);
set.add(2);
set.add(8);
set.add(1);
System.out.println(set.first() + " " + set.last());
TreeSet stores elements in sorted order. first() returns smallest (1), last() returns largest (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());
map.put("Z", "Last");
map.put("A", "First");
map.put("M", "Middle");
System.out.println(map.keySet());
LinkedHashMap maintains insertion order. Keys are returned in the order they were inserted: Z, A, M.
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());
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());
list2 references same object as list1 (size=3). list3 is a copy created before adding "C" (size=2).
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() + " ");
}
pq.add(5);
pq.add(1);
pq.add(3);
pq.add(2);
while(!pq.isEmpty()) {
System.out.print(pq.poll() + " ");
}
PriorityQueue returns elements in natural sorted order when polled. Smallest elements come out first.