Java Topic Practice
ArrayList Traversal
Practice mode with 15 Java-focused questions, immediate answer checks, and explanations.
For practice use only.
ArrayList Traversal MCQ Practice
ArrayList Traversal Java practice with 15 questions, code tracing, and a worked explanation for every answer.
Answered 0 of 15
Choose one answer.
Code Tracing 01 - ArrayList Traversal: track the final printed value.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(3);
nums.add(7);
nums.add(3);
nums.add(9);
nums.add(5);
int total = 0;
for (int i = 0; i < nums.size(); i += 2) {
total += nums.get(i);
}
System.out.print(total);
}
}All 15 arraylist traversal questions
Work through the interactive quiz above first. This is the full arraylist traversal question set with worked solutions, so students can review any question after attempting it.
1.Code Tracing 01 - ArrayList Traversal: track the final printed value.
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<Integer> nums = new ArrayList<Integer>(); nums.add(3); nums.add(7); nums.add(3); nums.add(9); nums.add(5); int total = 0; for (int i = 0; i < nums.size(); i += 2) { total += nums.get(i); } System.out.print(total); } }- 11
- 27
- 18
- 3
Show worked solution
Correct answer: 11
Stepping the index by 2 visits only the even positions of the list.
2.Code Tracing 12 - ArrayList Traversal: follow the variable updates.
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<Integer> nums = new ArrayList<Integer>(); nums.add(5); nums.add(9); nums.add(5); nums.add(11); nums.add(7); int total = 0; for (int v : nums) { total += v; } System.out.print(total); } }- 7
- 37
- 5
- 32
Show worked solution
Correct answer: 37
The enhanced for loop reads every element in order, and each Integer is automatically unboxed into the int accumulator.
3.Code Tracing 28 - ArrayList Traversal: trace the branch and loop path.
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<Integer> nums = new ArrayList<Integer>(); nums.add(8); nums.add(8); nums.add(3); nums.add(11); nums.add(10); for (int i = 0; i < nums.size(); i++) { if (nums.get(i) % 2 == 0) nums.remove(i); } System.out.print(nums); } }- [8, 8, 10]
- [8, 3, 11]
- [3, 11]
- [8, 8, 3, 11, 10]
Show worked solution
Correct answer: [8, 3, 11]
Removing while moving forward shifts the next element into the current index, so the loop skips past it.
4.Code Tracing 39 - ArrayList Traversal: watch the index changes.
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<Integer> nums = new ArrayList<Integer>(); nums.add(10); nums.add(10); nums.add(5); nums.add(10); nums.add(6); for (int i = nums.size() - 1; i >= 0; i--) { if (nums.get(i) % 2 == 0) nums.remove(i); } System.out.print(nums); } }- [10, 10, 10, 6]
- The code does not compile.
- [5]
- [10, 10, 5, 10, 6]
Show worked solution
Correct answer: [5]
Walking backwards means a removal only shifts elements the loop has already passed, so nothing gets skipped.
5.Code Tracing 50 - ArrayList Traversal: evaluate the state change step by step.
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<Integer> nums = new ArrayList<Integer>(); nums.add(12); nums.add(8); nums.add(7); nums.add(9); nums.add(8); for (int v : nums) { if (v == 8) nums.remove(Integer.valueOf(v)); } System.out.print(nums); } }- [12, 7, 9]
- [12, 8, 7, 9, 8]
- (no output)
- A runtime exception is thrown.
Show worked solution
Correct answer: A runtime exception is thrown.
Structurally changing a list while an enhanced for loop is iterating it throws ConcurrentModificationException.
6.What is printed by the following code segment?
ArrayList<Integer> l = new ArrayList<Integer>(); for (int i = 1; i <= 4; i++) { l.add(i * i); } int s = 0; for (int i = 1; i < l.size(); i++) { s += l.get(i) - l.get(i - 1); } System.out.print(s);- 15
- 30
- 10
- 16
Show worked solution
Correct answer: 15
Consecutive differences telescope, so the total equals the last element minus the first.
7.What is printed by the following code segment?
ArrayList<String> l = new ArrayList<String>(); l.add("aa"); l.add("b"); l.add("ccc"); String out = ""; for (String v : l) { out = v.length() > out.length() ? v : out; } System.out.print(out);- aa
- ccc
- b
- aabccc
Show worked solution
Correct answer: ccc
The conditional keeps whichever string is longer as the traversal proceeds.
8.What is printed by the following code segment?
ArrayList<Integer> l = new ArrayList<Integer>(); l.add(4); l.add(8); l.add(2); for (int i = 0; i < l.size(); i++) { l.set(i, l.get(i) / 2); } System.out.print(l);- [4, 8, 2]
- [2, 4, 2]
- [2, 4, 1]
- [8, 16, 4]
Show worked solution
Correct answer: [2, 4, 1]
Calling set replaces each element in place without changing the size.
9.What is printed by the following code segment?
ArrayList<Integer> l = new ArrayList<Integer>(); l.add(1); l.add(2); l.add(3); String out = ""; for (int i = l.size() - 1; i >= 0; i--) { out += l.get(i); } System.out.print(out);- 123
- 3
- 1
- 321
Show worked solution
Correct answer: 321
An indexed loop can run backward, which the enhanced form cannot do.
10.What is printed by the following code segment?
ArrayList<Integer> l = new ArrayList<Integer>(); l.add(5); l.add(3); l.add(9); l.add(1); int idx = 0; for (int i = 1; i < l.size(); i++) { if (l.get(i) < l.get(idx)) { idx = i; } } System.out.print(idx + " " + l.get(idx));- 3 1
- 1 3
- 2 9
- 0 5
Show worked solution
Correct answer: 3 1
The loop tracks the position of the smallest element rather than the value.
11.What is printed by the following code segment?
ArrayList<String> l = new ArrayList<String>(); l.add("cat"); l.add("cow"); l.add("dog"); int c = 0; for (String v : l) { if (v.charAt(0) == 'c') { c++; } } System.out.print(c);- 3
- 2
- 1
- 0
Show worked solution
Correct answer: 2
Comparing a char with == is correct, and two elements start with the letter c.
12.What is printed by the following code segment?
ArrayList<Integer> a = new ArrayList<Integer>(); a.add(1); a.add(2); ArrayList<Integer> b = new ArrayList<Integer>(); for (int v : a) { b.add(v * 10); } b.add(99); System.out.print(a.size() + " " + b.size());- 3 3
- 2 2
- 2 3
- 3 2
Show worked solution
Correct answer: 2 3
Building a second list leaves the first untouched, so only the copy grows.
13.What is printed by the following code segment?
ArrayList<Integer> l = new ArrayList<Integer>(); for (int i = 1; i <= 6; i++) { l.add(i); } int s = 0; for (int i = 1; i < l.size(); i += 2) { s += l.get(i); } System.out.print(s);- 9
- 21
- 6
- 12
Show worked solution
Correct answer: 12
Stepping by two from index 1 collects the elements 2, 4, and 6.
14.What is printed by the following code segment?
ArrayList<Integer> l = new ArrayList<Integer>(); l.add(2); l.add(2); l.add(5); int runs = 1; for (int i = 1; i < l.size(); i++) { if (!l.get(i).equals(l.get(i - 1))) { runs++; } } System.out.print(runs);- 2
- 3
- 1
- 5
Show worked solution
Correct answer: 2
Using equals rather than == is essential because the elements are Integer objects.
15.What is printed by the following code segment?
ArrayList<Integer> l = new ArrayList<Integer>(); int s = 0; for (int v : l) { s += v; } System.out.print(l.size() + " " + s + " " + l.isEmpty());- 0 0 false
- 0 0 true
- An exception is thrown.
- 0 null true
Show worked solution
Correct answer: 0 0 true
Traversing an empty list simply never enters the body.
