🎙 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 7 of 10

What does this print? (code)

What does this print?

class Shape {
    Shape() { show(); }
    void show() { System.out.print("shape; "); }
}

class Circle extends Shape {
    int radius = 7;
    @Override
    void show() { System.out.print("radius " + radius + "; "); }
}

public class Main {
    public static void main(String[] args) {
        new Circle().show();
        System.out.println("done");
    }
}
  1. A.shape; radius 7; done
  2. B.radius 7; radius 7; done
  3. C.radius 0; radius 7; done
  4. D.shape; shape; done
Answer & explanation

Correct answer: radius 0; radius 7; done

During Shape() the overridden show() runs before radius is set (0); after construction it is 7.

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 →