A trace table lets you manually track how every variable changes as code executes, line by line — the single most reliable way to find a logical error or predict a program's output.
Click through the steps below to see exactly how a trace table is built for a simple summing loop.
Worked example — click through step by step:
int sum = 0;
for (int i = 1; i <= 4; i++) {
sum = sum + i;
}
System.out.println(sum);| i | sum |
|---|---|
| — | 0 |
Before the loop: sum is initialised to 0.
💡 Exam Tip
The most common trace-table mistake is getting the loop's exit condition wrong — always double-check whether the loop is a pre-check (condition tested BEFORE the body runs, e.g. for/while) or a post-check (tested AFTER, e.g. do…while) — this changes whether the loop body can ever run zero times.
Question 1
What value is printed after this loop finishes?
int sum = 0;
for (int i = 1; i <= 4; i++) {
sum = sum + i;
}
System.out.println(sum);Question 2
How many times does the loop body execute (what is the final value of count)?
int count = 0;
int n = 20;
while (n > 1) {
n = n / 2;
count++;
}
System.out.println(count);Question 3
What is printed after this code runs?
int a = 5, b = 8; int temp = a; a = b; b = temp; System.out.println(a + " " + b);
Question 4
What is the value of flag after this code runs?
int x = 7;
boolean flag = false;
if (x % 2 == 0) {
flag = true;
} else {
flag = false;
}
System.out.println(flag);Question 5
What is the final value of result?
int result = 100;
int num = 246;
while (num > 0) {
result = num % 10;
num = num / 10;
}
System.out.println(result);Question 6
What is printed? (Remember: continue skips the rest of that iteration only.)
int total = 0;
for (int i = 0; i < 5; i++) {
if (i == 3) {
continue;
}
total += i;
}
System.out.println(total);Question 7
What is printed after these nested loops finish?
int count = 0;
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
count++;
}
}
System.out.println(count);Question 8
What is the final value of highest?
int[] marks = {60, 75, 40, 90};
int highest = marks[0];
for (int i = 1; i < marks.length; i++) {
if (marks[i] > highest) {
highest = marks[i];
}
}
System.out.println(highest);Question 9
What is printed? (Remember: a do-while always runs its body at least once, checking the condition afterwards.)
int n = 3;
int total = 0;
do {
total += n;
n--;
} while (n > 0);
System.out.println(total);Question 10
What is printed after this code runs?
int x = 4;
int y = 9;
if (x > y) {
x = x + y;
} else if (x == y) {
x = 0;
} else {
y = y - x;
}
System.out.println(x + " " + y);Question 11
How many even numbers are counted in the array?
int[] nums = {3, 1, 4, 1, 5};
int even = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] % 2 == 0) {
even++;
}
}
System.out.println(even);Question 12
What is printed after this loop finishes?
String word = "exam";
String result = "";
for (int i = word.length() - 1; i >= 0; i--) {
result = result + word.charAt(i);
}
System.out.println(result);Question 13
What is printed after this code runs? (Careful — trace each assignment in order.)
int a = 2; int b = 3; int c = a; a = b; b = c + a; System.out.println(a + " " + b);
Question 14
What is the final value of total (sum of multiples of 3 from 1 to 10)?
int total = 0;
for (int i = 1; i <= 10; i++) {
if (i % 3 == 0) {
total += i;
}
}
System.out.println(total);