Exams and tests
What does this program print? Practise Java output questions
Published on October 22, 20265 min read
The exam shows a short Java program and a question below it: “What does this program print?” You read the code, it looks harmless, and still one line of your answer ends up wrong. This question comes up again and again, in class tests, in high school courses like AP Computer Science A and in university exams. Here you'll learn how to solve it step by step, which traps are almost always built in and how to practise them on purpose.
Why this question costs so many points
When you program on a computer, the computer tells you what comes out. In the exam, you are the computer. That's a different skill from writing code: you have to execute every line exactly the way Java executes it, not the way you meant it.
Most mistakes don't happen because someone doesn't understand loops. They happen because you take shortcuts in your head. You “see” that a loop runs four times and forget to work through the last pass. Or you miss that 7 / 2 gives 3 in Java. This question tests exactly those details.
How to approach it: step by step
- Start at
main. That's where the program begins. Methods above it only run when they are called. - Draw a trace table. Each variable gets a column, each change a new row.
- Execute every line on its own. No shortcuts, not even on the third pass through a loop.
- Write down the output immediately. Watch out for
print(no line break) andprintln(with a line break). - At the end, check spaces and lines. A missing space in
i + " " + jcan cost you marks.
Example with a trace table
public class Main { public static void main(String[] args) { int x = 1; for (int i = 0; i < 4; i++) { x = x * 2 + i; } System.out.println(x); }}
This is what the table looks like. You check the condition i < 4 before every pass:
| i | i < 4? | x afterwards |
|---|---|---|
| – | – | 1 |
| 0 | yes | 1 · 2 + 0 = 2 |
| 1 | yes | 2 · 2 + 1 = 5 |
| 2 | yes | 5 · 2 + 2 = 12 |
| 3 | yes | 12 · 2 + 3 = 27 |
| 4 | no | loop ends |
27
The table feels slow at first. In the exam it's faster than a second attempt, because you find a mistake straight away. If you're not yet sure how for and while run, read loops in Java first.
The traps that are almost always there
Examiners like to include spots where the code works differently from how it looks. Most of them belong to one of these groups.
Integer division and string concatenation
public class Main { public static void main(String[] args) { int a = 7; int b = 2; System.out.println(a / b); System.out.println(a % b); System.out.println(a / 2.0); System.out.println("Sum: " + a + b); System.out.println(a + b + " is the sum"); }}
3 1 3.5 Sum: 72 9 is the sum
7 / 2with twointvalues cuts off the decimals and gives3. Only with2.0does Java calculate with decimals.+is evaluated from left to right. Once there's a string on the left, everything after it is appended:"Sum: " + 7gives"Sum: 7", then2is added on. In the last line Java calculates7 + 2first, because there's no string on the left yet.
Strings don't change, arrays do
public class Main { static void change(int n, int[] values) { n = 99; values[0] = 99; }
public static void main(String[] args) { String s = "Java"; s.toUpperCase(); System.out.println(s);
int i = 5; int j = i++ + ++i; System.out.println(i + " " + j);
int n = 1; int[] values = {1, 2}; change(n, values); System.out.println(n + " " + values[0]); }}
Java 7 12 1 99
s.toUpperCase()returns a new string,sstays"Java". More on this in strings in Java.i++first returns the old value (5) and then increases it to 6.++ifirst increases it to 7 and returns 7. Soj = 5 + 7 = 12andi = 7.- For an
int, a method receives a copy of the value. For an array, it receives a reference to the same array. That's whynstays at 1, butvalues[0]becomes 99. How parameters work in detail is explained in methods and recursion.
More classics
- Loop bounds:
i < 5ori <= 5? Always work out the first and the last pass. - Conditions without braces: after
if (x > 3)without curly braces, only the next statement belongs to theif. See conditions in Java. - switch without break: if
breakis missing, the code falls through into the next cases. - Recursion: write each call on its own line and work back up from the bottom.
How to practise properly
Reading code alone isn't enough. A fixed routine works:
- Predict first. Write down your complete output before you run anything.
- Then compare. Run the program and compare line by line.
- Name the mistake. Was it integer division, one pass too many,
i++? Write it down. After a few tasks you'll see which trap keeps catching you. - Change the code. Swap
<for<=ori++for++iand predict the new output. - Practise on paper. In many exams you don't have a computer to run the code. How to prepare for that is in writing Java code on paper.
The order matters. If you run the code first and “follow along” afterwards, you only train recognition. ChatGPT can explain an output to you, but you have to predict it yourself. How to use AI sensibly here is covered in learning Java with ChatGPT.
Output tasks in Jario
Jario has its own tasks of the type “What does the program print?”. You see a program, type in your prediction and then compare it with the real output. That fits the routine above exactly: think first, then check.
On top of that there's the editor in the browser. You can paste any example from this post into it, change a line and see what happens immediately with “Run”. You don't need to install anything. If you don't understand why the output differs from what you expected, the AI tutor gives you a hint in small steps instead of telling you the answer. The tasks range from “Easy” to “Hard”, and the first two chapters are free with a free account.
Which topics to practise in which order, so that no trap catches you off guard, is shown in the right order for learning Java.
In short
- Start at
mainand execute every line on its own. - Use a trace table, especially for loops.
- Watch out for integer division, string concatenation,
i++and++i, loop bounds and references to arrays. - Always practise in this order: predict, run, compare, name the mistake.
Related articles to learn
- LoopsJava loops explained simply: for, while, do-while, break and continue with examples. Plus the common mistakes that cost points in exams.Read article
- StringsJava strings explained simply: substring, charAt, split, equals and StringBuilder with examples. Plus common mistakes like comparing with ==.Read article
- Methods and recursionJava methods explained simply: parameters, return, overloading, varargs and recursion with examples. Plus common mistakes like a missing return.Read article
More posts
- Exams and testsAP CSA in 3 weeksAP Computer Science A in 3 weeks: what the exam looks like, a week-by-week Java study plan, and how to write free-response code without an IDE.Read more
- Exams and testsAssignments without all-nightersWeekly programming assignments without all-nighters: how to spread one over the week, test your code before you submit and get help without plagiarism.Read more
- Exams and testsOracle Java certificationOracle Java SE 21 Developer exam 1Z0-830: format, topics, who it is for, how to prepare and whether it is worth it. Plus what changes with Java 25.Read more