← 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");
}
}
- A.shape; radius 7; done
- B.radius 7; radius 7; done
- C.radius 0; radius 7; done
- 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 →