Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q4 of 15
C Sharp (C#) - Polymorphism and Abstract Classes
What will be the output of this code?
abstract class Shape {
    public abstract string Name();
}
class Circle : Shape {
    public override string Name() => "Circle";
}
class Program {
    static void Main() {
        Shape s = new Circle();
        System.Console.WriteLine(s.Name());
    }
}
AShape
BRuntime error
CCompilation error
DCircle
Step-by-Step Solution
Solution:
  1. Step 1: Understand method overriding in abstract classes

    The abstract method Name() in Shape is overridden in Circle to return "Circle".
  2. Step 2: Trace program execution

    Creating a Circle object and calling Name() prints "Circle".
  3. Final Answer:

    Circle -> Option D
  4. Quick Check:

    Overridden method call returns subclass string [OK]
Quick Trick: Overridden abstract methods run subclass code [OK]
Common Mistakes:
MISTAKES
  • Expecting abstract class method output
  • Thinking abstract classes can be instantiated
  • Confusing compile and runtime errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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