Java Level 1 Test

Naveen AnamaneniUpdated · 8 min read

A swimming instructor doesn't move a child to the deep end because they have attended ten lessons. They ask the child to swim one length on their own, and then they decide. This test is your length of the pool for the first level of the course. It covers how Java runs, data types, operators, decisions, loops, arrays and methods. If you pass it, you are ready for objects and classes. If you don't, each answer tells you exactly which lesson to read again.

How the Test Works

  • What's in it: 20 multiple-choice questions, then 3 output puzzles where you work out what a short program prints.
  • Time: 30 minutes, without notes, an IDE or JShell. Answer on paper first; run code only to check afterwards.
  • Pass mark: 16 out of 23. The quiz marks the 20 questions for you. Mark the puzzles yourself, one point each, and add them to your quiz score.
  • What it covers: the lessons from "What Is Java" to "Recursion in Java".

Part 1: 20 Questions

Level 1 test: 20 questions

20 questions
1Which part of the JVM turns frequently run bytecode into native machine code?
2What file does javac Hello.java produce?
3Which of these is not a primitive type?
4What is the default value of an int instance field?
5What is (int) 3.99?
6What is the value of b after byte b = (byte) 130;?
7What does System.out.println(1 + 2 + "3" + 4 + 5); print?
8After int x = 5; int y = x++ + ++x;, what is y?
9Which loop always runs its body at least once?
10What does System.out.println(0.1 + 0.2 == 0.3); print?
11After char c = 'A'; c += 2;, what is c?
12Which is a valid identifier?
13Where can var be used?
14In a switch expression, which keyword returns a value from a block { } arm?
15What happens with int[] a = new int[3]; System.out.println(a[3]);?
16How does Java pass arguments to methods?
17What is Math.round(-2.5)?
18What does System.out.println(5.0 / 0); print?
19What does int i = 0; if (false && ++i > 0) { } System.out.println(i); print?
20Which command runs a single .java file without a separate javac step?

Part 2: Output Puzzles

Work out what each program prints, on paper, before you open the solution. The solution shows the full program, its real output and a Try it button. Give yourself one point for each puzzle you get exactly right.

Puzzle 1: Postfix Twice

What is printed?

Java
int i = 10;
i = i++ + i--;
System.out.println(i);
Show solution
PostfixPuzzle.java▶ Try it
public class PostfixPuzzle {
    public static void main(String[] args) {
        int i = 10;
        i = i++ + i--;
        System.out.println(i);
    }
}
Output
21
Why this output?

i++ gives 10 and makes i 11. Then i-- gives 11 and makes i 10. The sum, 10 + 11 = 21, is assigned to i last, so it replaces the 10. Revise: Increment and Decrement Operators.

Puzzle 2: Continue and Break

What is printed?

Java
for (int i = 0; i < 5; i++) {
    if (i == 3) continue;
    if (i == 4) break;
    System.out.print(i);
}
Show solution
LoopPuzzle.java▶ Try it
public class LoopPuzzle {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            if (i == 3) continue;
            if (i == 4) break;
            System.out.print(i);
        }
        System.out.println();
    }
}
Output
012
Why this output?

The loop prints 0, 1 and 2. When i is 3, continue skips the print and goes to the next round. When i is 4, break ends the loop before anything is printed. Revise: Java Continue Statement.

Puzzle 3: Changing an Array in a Method

What is printed?

Java
public static void main(String[] args) {
    int[] a = {1, 2, 3};
    change(a);
    System.out.println(a[0]);
}
 
static void change(int[] arr) {
    arr[0] = 99;
    arr = new int[] {7, 7, 7};
}
Show solution
ArrayPuzzle.java▶ Try it
public class ArrayPuzzle {
    public static void main(String[] args) {
        int[] a = {1, 2, 3};
        change(a);
        System.out.println(a[0]);
    }
 
    static void change(int[] arr) {
        arr[0] = 99;
        arr = new int[] {7, 7, 7};
    }
}
Output
99
Why this output?

arr starts as a copy of the reference in a, so both point at the same array, and arr[0] = 99 changes that shared array. The next line only points the method's own copy, arr, at a new array. a in main never sees it. Revise: Call by Value and Call by Reference in Java.

What Your Score Means

Score (out of 23)What it meansWhat to do next
20 to 23You know the basics wellMove on to Classes and Objects
16 to 19A pass, with a few gapsRead the lessons linked from the questions you missed, then move on
Under 16Not yetReread the lessons behind your wrong answers, redo the 20 Basic Java Programs for Practice, and take the test again

Was this tutorial helpful?