Bird
0
0

What is the error in the following code?

medium📝 Debug Q6 of 15
C Sharp (C#) - Interfaces
What is the error in the following code?
interface IRunner { void Execute(); }
class Runner : IRunner { public void IRunner.Execute() { Console.WriteLine("Running"); } }
AThe class must be declared abstract to implement interface explicitly.
BThe method name should be 'Run' instead of 'Execute'.
CThe interface name should not be prefixed in the method implementation.
DExplicit interface methods cannot have access modifiers like 'public'.
Step-by-Step Solution
Solution:
  1. Step 1: Recall explicit interface implementation rules

    Explicit interface methods must not include access modifiers such as 'public'.
  2. Step 2: Check the code

    The method is declared as public void IRunner.Execute(), which is invalid.
  3. Step 3: Evaluate options

    Explicit interface methods cannot have access modifiers like 'public'. correctly identifies the error. The method name should be 'Run' instead of 'Execute'. is irrelevant to the error. The interface name should not be prefixed in the method implementation. is incorrect because explicit implementation requires interface prefix. The class must be declared abstract to implement interface explicitly. is false; class need not be abstract.
  4. Final Answer:

    Explicit interface methods cannot have access modifiers like 'public'. -> Option D
  5. Quick Check:

    Explicit methods omit access modifiers [OK]
Quick Trick: Explicit interface methods must not have access modifiers [OK]
Common Mistakes:
MISTAKES
  • Adding 'public' or other access modifiers to explicit methods
  • Removing interface prefix in explicit implementation
  • Confusing method names with interface declaration

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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