Skip to lesson

Practice: Output Prediction & Debugging Mock Test

Placement tests and live rounds often hand you a block of short code snippets and a clock. This mock copies that: 15 predict-the-output questions (12 in the MCQ tab, 3 in Practice), two compile-error-or-output questions and five find-the-bug methods. It checks accuracy under time pressure, not just knowledge.

How to take this mock test

Exact output means the same characters, spaces, brackets and line breaks that the program prints. In a written round, one wrong space or missing bracket can cost the whole mark.

Treat it like the real round. Give yourself 20 minutes for the 15 predict-the-output questions (MCQ tab 1–12, then Practice items 1, 4 and 9) and 25 minutes for the two compile-error-or-output questions and the five bug fixes. No IDE and no running code until time is up.

For every snippet, first ask "does this compile?", then trace with a variable table. Write the exact output, including spaces and brackets. Skip a question after 90 seconds and come back to it.

  • Part A, 20 minutes: 15 predict-the-output (MCQ 1–12, Practice 1, 4, 9).
  • Part B, 25 minutes: compile-error-or-output (Practice 2, 5) and five bugs (Practice 3, 6, 7, 8, 10).
  • For each bug, write cause, fix and one test before checking.
  • Only after time is up: run the code and mark your answers.

Order of execution

Your routine for each question

Tap the steps in the order a strong candidate does them.

Compile error or output? Decide first

Definite assignment is the compiler rule that a local variable must be given a value on every path before it is read. Otherwise javac reports "variable might not have been initialized".

Many rounds add "Compile error" as an answer option, so check the compile rules before you trace. The program compiles because each line respects a rule; the comments show the versions that do not.

Example 1 · Lines that compile, and the versions that do not

CompileRulesDemo.javaJava
public class CompileRulesDemo {    static int score(int x) {        if (x > 0) {            return x;        }        return 0;    }
    public static void main(String[] args) {        byte b = 10;        b = (byte) (b * 2);        // b = b * 2;  -> possible lossy conversion from int to byte        int x = 10;        if (x == 10) {             // if (x = 10) -> int cannot be converted to boolean            System.out.println("ten");        }        String s = "set";          // String s; println(s) -> variable s might not have been initialized        System.out.println(s + " " + b + " " + score(-4));        final int[] arr = {1, 2};        arr[0] = 9;        System.out.println(arr[0]);    }}
Output
ten
set 20 0
9

What this code does

  • Each commented-out version is a classic "compile error" answer; the real javac message is in the comment.
  • final on an array variable stops reassignment (arr = new int[3]), but the elements can still change, so this line compiles and prints 9.

A beautifully traced output is worth nothing if the code does not compile. The rules to check first:

  • Types: an int into a byte without a cast; an int used as a condition (if (x = 10)).
  • Definite assignment: a local variable read before it is set.
  • Reachability: code after return, a catch for a checked exception that cannot be thrown, a parent catch before its child.
  • Overriding: weaker access, @Override on a static method, a final method.

Revise: Type conversion and casting (opens in a new tab).

Fix the error

Compile error: pick the fix

Read the error, then pick the line that fixes it.

Round 1 of 2

Doubling a byte value.

public class Doubling {
public static void main(String[] args) {
byte b = 10;
b = b * 2;
System.out.println(b);
}
}

Error

Doubling.java:4: error: incompatible types: possible lossy conversion from int to byte

Scoring guide

Score yourself honestly: one point per predict-the-output answer that matches exactly, and up to two points per bug (one for the right fix, one for a clear cause plus test). Compile-error questions are one point each. That is 15 + 2 + 10 = 27 points.

  • 24–27: ready for written rounds; spend time on speed.
  • 18–23: solid; redo the lessons for the topics you missed.
  • 12–17: trace every snippet with a table until it is automatic.
  • Below 12: revisit the four puzzle lessons of this module first.

Review: the most-missed traps

These traps cost the most marks. The program reruns the five most-missed ones, so you can see each answer again; if you lost a point, re-read its lesson.

Example 2 · The five most-missed traps, rerun

MostMissedDemo.javaJava
import java.util.ArrayList;import java.util.List;
class Base {    Base() {        show();    }
    void show() {    }}
class Child extends Base {    int value = 5;
    @Override    void show() {        System.out.println("3: child sees value = " + value);    }}
public class MostMissedDemo {    static int f() {        int x = 10;        try {            return x;        } finally {            x = 20;        }    }
    public static void main(String[] args) {        int i = 5;        i = i++;        System.out.println("1: i = " + i + ", " + (1 + 2 + "3" + 4 + 5));
        Integer a = 128, b = 128;        String s = "Java";        s.concat(" 17");        System.out.println("2: " + (a == b) + " " + s);
        new Child();
        System.out.println("4: f() = " + f());
        List<Integer> list = new ArrayList<>(List.of(10, 20, 30));        list.remove(1);        System.out.println("5: " + list);    }}
Output
1: i = 5, 3345
2: false Java
3: child sees value = 0
4: f() = 10
5: [10, 30]

What this code does

  • 1 and 2: i = i++ keeps 5, 1 + 2 + "3" adds before joining, 128 is outside the Integer cache, and concat without assignment does nothing.
  • 3 to 5: the parent constructor runs the child's show() before value is set, finally cannot change the saved 10, and remove(1) removes the element at index 1.

Find your trap in this list, then write one new puzzle of your own that uses it:

  • x++ uses the old value; i = i++ changes nothing; + with Strings groups left to right.
  • == on Strings and on Integer outside -128 to 127; concat without assignment.
  • Static methods and fields follow the declared type; a parent constructor calling an overridden method sees default values.
  • finally cannot change a returned primitive; remove(1) on a List<Integer> removes an index.

Revise: String puzzles (opens in a new tab), operator puzzles (opens in a new tab), try, catch and finally puzzles (opens in a new tab) and static and overriding puzzles (opens in a new tab).

Predict the output

Three most-missed traps

Read each program and pick what it prints.

Round 1 of 3

remove with a number on a List of Integers.

import java.util.ArrayList;
import java.util.List;
public class RemoveRound {
public static void main(String[] args) {
List<Integer> l = new ArrayList<>(List.of(1, 2, 3));
l.remove(Integer.valueOf(1));
l.remove(1);
System.out.println(l);
}
}

Next step

Check your understanding

12 quick questions