Code tracing with a table, even for short code
Your brain reads code for intent and fills in what it expects. Code tracing with a table forces you to execute what is written. It takes perhaps thirty seconds for a ten-line snippet and removes almost all careless errors.
int x = 10;
int count = 0;
while (x > 1) {
x = x / 3;
count++;
}
System.out.println(x + " " + count);
| Step | x | count | x > 1? |
|---|
| start | 10 | 0 | yes |
| after pass 1 | 3 | 1 | yes |
| after pass 2 | 1 | 2 | no, exit |
The output is 1 2. A candidate who reads instead of traces often thinks "10 divided by 3 is 3.33" and picks an option with a decimal, which is exactly the distractor the setter planted.
Integer division and remainder follow the language
Output prediction questions lean hard on division because the rules differ between languages, and candidates carry one language's habits into another.
| Expression | Java, C, C++ | Python 3 |
|---|
7 / 2 | 3 | 3.5 |
7 // 2 | // starts a comment | 3 |
-7 / 2 | -3 (truncates towards zero) | -3.5 |
-7 // 2 | // starts a comment | -4 (floors) |
-7 % 2 | -1 | 1 |
The negative rows are the ones assessments like. Java and C truncate towards zero (C has required this since C99), so the remainder takes the sign of the dividend. Python floors, so its remainder takes the sign of the divisor.
String concatenation is the same kind of rule. In Java and JavaScript, + evaluates left to right, so 1 + 2 + "3" gives "33" while "1" + 2 + 3 gives "123". Once a string appears, everything after it is concatenated.
References versus copies
The second big source of wrong answers is assuming an assignment copies an object. In Java, everything is passed by value, but for an object or array the value passed is the reference. The method can change the object through it, and reassigning the parameter changes nothing for the caller.
static void change(int[] arr, int n) {
arr[] = ;
n = ;
arr = []{};
}
[] a = {, };
;
change(a, n);
System.out.println(a[] + + n);
Python behaves the same way with names. After b = a on a list, b.append(3) changes the one list both names refer to. The page on mutable default arguments shows the same mechanism appearing somewhere less obvious.
Count loop iterations before tracing the body
Off-by-one errors come from tracing the body before you know how many times it runs. Read the bounds first. for (int i = 0; i <= 5; i++) runs six times, not five, so summing i gives 15; with i < 5 it gives 10, and both numbers will be among the options. Python's range(1, 6, 2) produces 1, 3 and 5 and stops before 6. If the body computes total += i * i, the answer is 1 + 9 + 25 = 35.
When the right option is not a printed value
The trap in output prediction that separates strong candidates is trusting the literal code over the expected shape of the question. The question says "what does this print", so candidates assume something prints. Sometimes the correct option is "compile error" or "undefined".
Evaluation order shows this well. In Java, int i = 5; int j = i++ + ++i; is well defined: operands evaluate left to right, i++ yields 5 and makes i 6, then ++i makes i 7 and yields 7, so j is 12. The same line in C modifies i twice without sequencing, which is undefined behaviour. In a C question, "undefined" is the correct option, and a precise number is wrong however carefully you traced it.
Compile errors hide the same way. Java rejects reading a local variable that might not have been assigned, so a snippet that looks like it prints 0 may not compile at all. Check declarations and types before you trace any values.
Going faster without guessing
Speed in output prediction comes from tracing less, not tracing carelessly. Read the options first: if they are 3, 3.5 and 4, the question is about division and only one line needs care. Find that line, trace it exactly, and treat the rest quickly. If two options differ only by one iteration, recount the loop bounds. Under a timer, quick code tracing on paper is still faster than reading the snippet three times.
Read the options to find the rule being tested, then let the trace table, not your intuition, choose the answer.
© 2026 Preptima. Originally published at preptima.com (opens in a new tab).