Bird
0
0

Given the code below, what will be the output?

medium📝 Predict Output Q4 of 15
C Sharp (C#) - Interfaces
Given the code below, what will be the output?
interface IShape { void Draw(); }
abstract class ShapeBase : IShape {
    public abstract void Draw();
    public void Display() { Console.WriteLine("Displaying shape"); }
}
class Circle : ShapeBase {
    public override void Draw() { Console.WriteLine("Drawing Circle"); }
}

var c = new Circle();
c.Draw();
c.Display();
ADrawing Circle
BDisplaying shape Drawing Circle
CCompile-time error
DDrawing Circle Displaying shape
Step-by-Step Solution
Solution:
  1. Step 1: Analyze class implementations

    Circle overrides Draw() to print 'Drawing Circle'. Display() is inherited and prints 'Displaying shape'.
  2. Step 2: Trace method calls

    Calling c.Draw() prints 'Drawing Circle', then c.Display() prints 'Displaying shape'.
  3. Final Answer:

    Drawing Circle Displaying shape -> Option D
  4. Quick Check:

    Abstract class methods run as implemented [OK]
Quick Trick: Abstract class can have implemented methods called normally [OK]
Common Mistakes:
MISTAKES
  • Assuming interface methods have implementation
  • Expecting Display() to be abstract
  • Thinking code causes compile error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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