Java Level 1 Test
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
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?
int i = 10;
i = i++ + i--;
System.out.println(i);Show solution
public class PostfixPuzzle {
public static void main(String[] args) {
int i = 10;
i = i++ + i--;
System.out.println(i);
}
}21Why 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?
for (int i = 0; i < 5; i++) {
if (i == 3) continue;
if (i == 4) break;
System.out.print(i);
}Show solution
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();
}
}012Why 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?
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
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};
}
}99Why 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 means | What to do next |
|---|---|---|
| 20 to 23 | You know the basics well | Move on to Classes and Objects |
| 16 to 19 | A pass, with a few gaps | Read the lessons linked from the questions you missed, then move on |
| Under 16 | Not yet | Reread the lessons behind your wrong answers, redo the 20 Basic Java Programs for Practice, and take the test again |
Was this tutorial helpful?