Bird
Raised Fist0

What will be printed when the following C# code is executed?

medium📝 Predict Output Q4 of Q15
C Sharp (C#) - Classes and Objects
What will be printed when the following C# code is executed?
class ScoreTracker {
  private int score = 0;
  public void AddPoint() { score += 2; }
  public int GetScore() { return score; }
}

var tracker = new ScoreTracker();
tracker.AddPoint();
tracker.AddPoint();
Console.WriteLine(tracker.GetScore());
A2
B1
C0
D4
Step-by-Step Solution
Solution:
  1. Step 1: Analyze AddPoint method

    Each call adds 2 to 'score'.
  2. Step 2: Calculate total after two calls

    2 + 2 = 4.
  3. Step 3: GetScore returns current score

    So, output is 4.
  4. Final Answer:

    4 -> Option D
  5. Quick Check:

    Two increments of 2 equals 4 [OK]
Quick Trick: AddPoint adds 2 each call; two calls total 4 [OK]
Common Mistakes:
MISTAKES
  • Assuming AddPoint adds 1 instead of 2
  • Forgetting to call AddPoint twice
  • Confusing initial score with updated score

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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