Java Level 2 Test
Before a driving test, most learners can steer, brake and park. What the examiner checks is whether they can do all of it together, in the right order, without someone beside them saying what comes next. Object-oriented Java is similar. You may know each idea on its own: constructors, inheritance, overriding, interfaces. This test checks whether you can tell which rule applies when they meet in the same few lines of code.
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.
- 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 "Classes and Objects" to "SOLID Principles in Java" and the design patterns.
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: Who Prints First
What does new B(); new B(); print?
class A {
static { System.out.print("1"); }
{ System.out.print("2"); }
A() { System.out.print("3"); }
}
class B extends A {
static { System.out.print("4"); }
{ System.out.print("5"); }
B() { System.out.print("6"); }
}Show solution
class A {
static { System.out.print("1"); }
{ System.out.print("2"); }
A() { System.out.print("3"); }
}
class B extends A {
static { System.out.print("4"); }
{ System.out.print("5"); }
B() { System.out.print("6"); }
}
public class InitOrderPuzzle {
public static void main(String[] args) {
new B();
new B();
System.out.println();
}
}1423562356Why this output?
The static blocks run once, parent first: 1, then 4. Then, for each object, the parent part is built before the child part: the parent's instance block and constructor (2, 3), then the child's instance block and constructor (5, 6). The second object repeats only 2356, because static blocks never run twice. Revise: Order of Initialization.
Puzzle 2: Same Name in Both Classes
What is printed?
class Parent {
String name = "Parent";
String show() { return "Parent"; }
}
class Child extends Parent {
String name = "Child";
String show() { return "Child"; }
}
// in main:
Parent p = new Child();
System.out.println(p.name + " " + p.show());Show solution
class Parent {
String name = "Parent";
String show() { return "Parent"; }
}
class Child extends Parent {
String name = "Child";
String show() { return "Child"; }
}
public class FieldPuzzle {
public static void main(String[] args) {
Parent p = new Child();
System.out.println(p.name + " " + p.show());
}
}Parent ChildWhy this output?
Fields are never overridden. p.name is chosen from the declared type, Parent. Methods are overridden, so p.show() runs the version of the real object, a Child. Revise: Runtime Polymorphism and Dynamic Method Dispatch.
Puzzle 3: Passing Null to an Overloaded Method
What is printed?
static void m(Object o) { System.out.println("Object"); }
static void m(String s) { System.out.println("String"); }
public static void main(String[] args) {
m(null);
}Show solution
public class OverloadPuzzle {
static void m(Object o) { System.out.println("Object"); }
static void m(String s) { System.out.println("String"); }
public static void main(String[] args) {
m(null);
}
}StringWhy this output?
null fits both methods. When more than one overload fits, the compiler picks the most specific one, and String is more specific than Object. If there were also an m(Integer), the call would be ambiguous and wouldn't compile. Revise: Method Overloading in Java.
What Your Score Means
| Score (out of 23) | What it means | What to do next |
|---|---|---|
| 20 to 23 | You think in objects | Move on to Strings in Java |
| 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, build a small class hierarchy of your own, and take the test again |
Was this tutorial helpful?