Bird
0
0

Consider this code:

hard📝 Application Q9 of 15
Java - Constructors
Consider this code:
class Base {
  Base() { System.out.print("Base"); }
  Base(int x) { System.out.print("Base" + x); }
}
class Derived extends Base {
  Derived() { super(5); System.out.print("Derived"); }
}
What is the output of new Derived();?
ADerivedBase5
BBase5Derived
CBaseDerived
DBase5
Step-by-Step Solution
Solution:
  1. Step 1: Identify which Base constructor is called

    Derived constructor calls super(5); so Base(int x) runs, printing "Base5".
  2. Step 2: Output from Derived constructor

    After Base prints, Derived prints "Derived".
  3. Final Answer:

    Base5Derived -> Option B
  4. Quick Check:

    Parameterized super call output = Base5Derived [OK]
Quick Trick: super(params) calls matching parent constructor [OK]
Common Mistakes:
  • Assuming default Base() runs
  • Expecting Derived before Base output
  • Ignoring parameter in super call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes