Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q5 of 15
C Sharp (C#) - Classes and Objects
What will be the output of this code?
class Counter {
  public int count = 0;
  public Counter() { count = 10; }
}

Counter c = new Counter();
Console.WriteLine(c.count);
Anull
B0
C10
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Analyze constructor behavior

    The constructor sets count to 10 when the object is created.
  2. Step 2: Determine printed value

    Printing c.count outputs 10, the value assigned in the constructor.
  3. Final Answer:

    10 -> Option C
  4. Quick Check:

    Constructor overrides default field value [OK]
Quick Trick: Constructor values override default field values [OK]
Common Mistakes:
MISTAKES
  • Expecting default 0 instead of constructor value
  • Confusing null with int default
  • Assuming compilation fails

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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