Java Level 2 Test

Naveen AnamaneniUpdated · 8 min read

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

Level 2 test: 20 questions

20 questions
1When new B() runs for the first time, which runs first?
2A class declares only the constructor A(int x) {}. What does new A() do?
3In Java 25, what may appear before an explicit super(...) call in a constructor?
4Can a static method be overridden?
5When is the overloaded method to call chosen?
6Which access level allows the same package and subclasses in other packages?
7An interface method with no body and no default, static or private modifier is implicitly:
8Fields declared in an interface are implicitly:
9What may an overriding method return?
10What does Integer c = 128, d = 128; System.out.println(c == d); print?
11What does the default Object.equals() compare?
12If a.equals(b) is true, then:
13Can an abstract class have a constructor?
14Which nested class does not need an instance of the outer class?
15Which is true of a record?
16Each permitted subclass of a sealed class must be declared:
17"Open for extension, closed for modification" is which SOLID principle?
18Which pattern builds a complex object step by step?
19What does Animal a = new Dog(); Cat c = (Cat) a; cause?
20Enum constructors are implicitly:

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?

Java
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
InitOrderPuzzle.java▶ Try it
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();
    }
}
Output
1423562356
Why 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?

Java
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
FieldPuzzle.java▶ Try it
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());
    }
}
Output
Parent Child
Why 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?

Java
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
OverloadPuzzle.java▶ Try it
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);
    }
}
Output
String
Why 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 meansWhat to do next
20 to 23You think in objectsMove on to Strings in Java
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, build a small class hierarchy of your own, and take the test again

Was this tutorial helpful?