Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q14 of 15
C Sharp (C#) - Inheritance
Identify the error in this code snippet:
class Base {
  public virtual void Show() { Console.WriteLine("Base"); }
}
class Derived : Base {
  public void Show() { Console.WriteLine("Derived"); }
}

Base obj = new Derived();
obj.Show();
ANo error; output is Base
BNo error; output is Derived
CCompile-time error: missing override keyword
DRuntime error: method not found
Step-by-Step Solution
Solution:
  1. Step 1: Check method overriding rules

    The derived class method Show does not use override, so it hides the base method instead of overriding.
  2. Step 2: Determine method called by base reference

    Because Show is virtual in base but not overridden, calling obj.Show() calls base class method, outputting "Base".
  3. Final Answer:

    No error; output is Base -> Option A
  4. Quick Check:

    Missing override means base method runs [OK]
Quick Trick: Override keyword needed to replace virtual method [OK]
Common Mistakes:
MISTAKES
  • Assuming method hides override automatically
  • Expecting Derived output without override
  • Thinking missing override causes compile error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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