Bird
Raised Fist0

What will be the output of the following C# code?

medium📝 Predict Output Q5 of Q15
C Sharp (C#) - Inheritance
What will be the output of the following C# code?
class Base {
    public Base() { Console.WriteLine("Base constructor"); }
}
class Derived : Base {
    public Derived() : base() { Console.WriteLine("Derived constructor"); }
}
static void Main() {
    Derived d = new Derived();
}
ADerived constructor\nBase constructor
BBase constructor\nDerived constructor
COnly Derived constructor
DOnly Base constructor
Step-by-Step Solution
Solution:
  1. Step 1: Understand constructor chaining

    When a derived class constructor is called, it first calls the base class constructor.
  2. Step 2: Analyze the output

    The base constructor prints "Base constructor" first, then the derived constructor prints "Derived constructor".
  3. Final Answer:

    Base constructor\nDerived constructor -> Option B
  4. Quick Check:

    Base constructor runs before derived [OK]
Quick Trick: Base constructor runs before derived constructor [OK]
Common Mistakes:
MISTAKES
  • Assuming derived constructor runs first
  • Ignoring implicit base constructor call
  • Thinking constructors run simultaneously

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes