Bird
Raised Fist0

What will be the output of this C# code?

medium📝 Predict Output Q4 of Q15
C Sharp (C#) - Polymorphism and Abstract Classes
What will be the output of this C# code?
abstract class Shape {
    public abstract double Area();
}
class Circle : Shape {
    private double radius = 3;
    public override double Area() {
        return 3.14 * radius * radius;
    }
}
class Program {
    static void Main() {
        Shape s = new Circle();
        System.Console.WriteLine(s.Area());
    }
}
A28.26
B0
CCompilation error
DRuntime error
Step-by-Step Solution
Solution:
  1. Step 1: Understand class hierarchy and method override

    Shape is abstract with an abstract method Area. Circle overrides Area to calculate area of circle with radius 3.
  2. Step 2: Calculate the area and output

    Area = 3.14 * 3 * 3 = 28.26. The program creates a Circle object as Shape reference and calls Area(), printing 28.26.
  3. Final Answer:

    28.26 -> Option A
  4. Quick Check:

    Abstract method override returns correct value [OK]
Quick Trick: Abstract methods must be overridden to avoid errors [OK]
Common Mistakes:
MISTAKES
  • Expecting abstract class to instantiate directly
  • Forgetting to override abstract method
  • Miscomputing the area formula

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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