Bird
0
0

What will be the output of this C# code?

medium📝 Predict Output Q13 of 15
C Sharp (C#) - Polymorphism and Abstract Classes
What will be the output of this C# code?
abstract class Animal {
    public abstract void Speak();
}

class Dog : Animal {
    public override void Speak() {
        Console.WriteLine("Woof");
    }
}

class Program {
    static void Main() {
        Animal a = new Dog();
        a.Speak();
    }
}
ARuntime error because Speak is abstract
BCompile-time error because Animal is abstract
CWoof
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Understand abstract method implementation

    The abstract method Speak in Animal is overridden in Dog with a concrete implementation that prints "Woof".
  2. Step 2: Analyze runtime behavior

    Creating an Animal reference to a Dog object and calling Speak() calls the overridden method, printing "Woof".
  3. Final Answer:

    Woof -> Option C
  4. Quick Check:

    Abstract method overridden = prints 'Woof' [OK]
Quick Trick: Abstract method must be overridden to run [OK]
Common Mistakes:
MISTAKES
  • Thinking abstract class cannot be referenced
  • Expecting compile or runtime error
  • Assuming abstract methods have bodies

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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