← Quiz: Inheritance and Polymorphism: all 10 questions
Quiz: Inheritance and Polymorphism · Question 6 of 10
What does this print? (code)
What does this print?
class A {
A() { System.out.print("A"); }
}
class B extends A {
B() { System.out.print("B"); }
}
public class Main {
public static void main(String[] args) {
new B();
System.out.println();
}
}
- A.B
- B.BA
- C.AB
- D.A
Answer & explanation
Correct answer: AB
Every constructor first calls the parent constructor (an automatic super()), so A's body runs before B's.
Can you get the rest right without peeking?
Take the timed Quiz: Inheritance and Polymorphism quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →