Bird
Raised Fist0

What output will this C# program produce?

medium📝 Predict Output Q5 of Q15
C Sharp (C#) - Inheritance
What output will this C# program produce?
class Widget {
  public Widget() : this(3, "Init") {
    Console.WriteLine("Default constructor");
  }
  public Widget(int x, string y) {
    Console.WriteLine($"Params: {x}, {y}");
  }
}

class Program {
  static void Main() {
    new Widget();
  }
}
ADefault constructor Params: 3, Init
BParams: 3, Init Default constructor
CParams: 3, Init
DDefault constructor
Step-by-Step Solution
Solution:
  1. Step 1: Analyze constructor chaining

    The parameterless constructor calls the two-parameter constructor first.
  2. Step 2: Determine output order

    The two-parameter constructor prints first: "Params: 3, Init". Then the parameterless constructor prints "Default constructor".
  3. Final Answer:

    Params: 3, Init Default constructor -> Option B
  4. Quick Check:

    Chained constructor executes before caller [OK]
Quick Trick: Chained constructor runs before the calling one [OK]
Common Mistakes:
MISTAKES
  • Reversing print order
  • Expecting only one constructor to print
  • Ignoring chaining call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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