← 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);
}
}
- A.Q5 Q P
- B.P Q5
- C.Q P Q5
- 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 →