🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Object Initialization Order: all 10 questions

Quiz: Object Initialization Order · Question 4 of 10

What does this print? (code)

What does this print?

class P {
    static String log = "";
    P() { log += "P "; }
}

class Q extends P {
    Q() { log += "Q "; }
    Q(int x) { this(); log += "Q" + x; }
}

public class Main {
    public static void main(String[] args) {
        new Q(5);
        System.out.println(P.log);
    }
}
  1. A.Q5 Q P
  2. B.P Q5
  3. C.Q P Q5
  4. D.P Q Q5
Answer & explanation

Correct answer: P Q Q5

Q(5) calls this(), whose hidden super() prints P first; then Q, then Q5.

Can you get the rest right without peeking?

Take the timed Quiz: Object Initialization Order quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →