Bird
0
0

What will be the output of the following code?

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

class Dog : Animal {
    public override string Speak() {
        return "Woof";
    }
}

class Program {
    static void Main() {
        Animal myDog = new Dog();
        System.Console.WriteLine(myDog.Speak());
    }
}
AWoof
BAnimal
CCompile-time error
DRuntime error
Step-by-Step Solution
Solution:
  1. Step 1: Understand class inheritance and method override

    Dog inherits from abstract Animal and implements the abstract Speak method returning "Woof".
  2. Step 2: Trace program execution

    Main creates a Dog object as Animal type and calls Speak(), which runs Dog's override returning "Woof".
  3. Final Answer:

    Woof -> Option A
  4. Quick Check:

    Override abstract method = Dog's Speak() output [OK]
Quick Trick: Abstract method calls run subclass override. [OK]
Common Mistakes:
MISTAKES
  • Expecting abstract class method output.
  • Thinking abstract classes can be instantiated.
  • Confusing compile-time and runtime errors.

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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