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

  1. Start at main. That's where the program begins. Methods above it only run when they are called.
  2. Draw a trace table. Each variable gets a column, each change a new row.
  3. Execute every line on its own. No shortcuts, not even on the third pass through a loop.
  4. Write down the output immediately. Watch out for print (no line break) and println (with a line break).
  5. At the end, check spaces and lines. A missing space in i + " " + j can 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:

ii < 4?x afterwards
––1
0yes1 · 2 + 0 = 2
1yes2 · 2 + 1 = 5
2yes5 · 2 + 2 = 12
3yes12 · 2 + 3 = 27
4noloop 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 / 2 with two int values cuts off the decimals and gives 3. Only with 2.0 does Java calculate with decimals.
  • + is evaluated from left to right. Once there's a string on the left, everything after it is appended: "Sum: " + 7 gives "Sum: 7", then 2 is added on. In the last line Java calculates 7 + 2 first, 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, s stays "Java". More on this in strings in Java.
  • i++ first returns the old value (5) and then increases it to 6. ++i first increases it to 7 and returns 7. So j = 5 + 7 = 12 and i = 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 why n stays at 1, but values[0] becomes 99. How parameters work in detail is explained in methods and recursion.

More classics

  • Loop bounds: i < 5 or i <= 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 the if. See conditions in Java.
  • switch without break: if break is 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:

  1. Predict first. Write down your complete output before you run anything.
  2. Then compare. Run the program and compare line by line.
  3. 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.
  4. Change the code. Swap < for <= or i++ for ++i and predict the new output.
  5. 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 main and 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.