🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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();
    }
}
  1. A.B
  2. B.BA
  3. C.AB
  4. 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 →