Bird
0
0

Examine the following C# code snippet and identify the issue:

medium📝 Debug Q7 of 15
C Sharp (C#) - Classes and Objects
Examine the following C# code snippet and identify the issue:
class Counter {
    public static int count = 0;
    public void Increment() {
        count++;
    }
    public static void Reset() {
        count = 0;
        Increment();
    }
}
ANo errors; code works as expected
BStatic field count cannot be modified inside instance method Increment()
CReset() should be an instance method to access count
DCannot call instance method Increment() from static method Reset()
Step-by-Step Solution
Solution:
  1. Step 1: Identify method types

    Increment() is an instance method; Reset() is static.
  2. Step 2: Understand method calls

    A static method cannot call an instance method directly because it lacks an instance context.
  3. Step 3: Analyze code

    Reset() calls Increment() without an instance, causing a compile-time error.
  4. Final Answer:

    Cannot call instance method Increment() from static method Reset() -> Option D
  5. Quick Check:

    Static methods can't call instance methods directly [OK]
Quick Trick: Static methods lack instance context [OK]
Common Mistakes:
MISTAKES
  • Assuming static methods can call instance methods without an object
  • Thinking static fields can't be modified in instance methods
  • Believing Reset() must be instance to access static fields

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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