Bird
0
0

What will be the output of this C# code?

medium📝 Predict Output Q13 of 15
C Sharp (C#) - Classes and Objects
What will be the output of this C# code?
class Counter {
  private int count = 0;
  public void Increment() { count++; }
  public int GetCount() { return count; }
}

var c = new Counter();
c.Increment();
c.Increment();
Console.WriteLine(c.GetCount());
A3
B1
C0
D2
Step-by-Step Solution
Solution:
  1. Step 1: Trace the Increment method calls

    Each call to Increment increases count by 1. Two calls increase count from 0 to 2.
  2. Step 2: Check the GetCount method output

    GetCount returns the current count, which is 2 after two increments.
  3. Final Answer:

    2 -> Option D
  4. Quick Check:

    2 increments = count 2 [OK]
Quick Trick: Count increments twice, so output is 2 [OK]
Common Mistakes:
MISTAKES
  • Forgetting that count starts at 0
  • Assuming Increment adds more than 1
  • Confusing method names or outputs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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