Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q5 of 15
C Sharp (C#) - Inheritance
What will be the output of this code?
sealed class FinalClass {
    public void Print() { Console.WriteLine("Hello"); }
}

class Test : FinalClass {
}

var t = new Test();
t.Print();
AHello
BNo output
CCompilation error
DRuntime error
Step-by-Step Solution
Solution:
  1. Step 1: Understand sealed class inheritance

    FinalClass is sealed, so it cannot be inherited by Test.
  2. Step 2: Check code validity

    Attempting to inherit from a sealed class causes a compilation error.
  3. Final Answer:

    Compilation error -> Option C
  4. Quick Check:

    Sealed class inheritance = compilation error [OK]
Quick Trick: Sealed classes cannot be base classes; inheritance causes error [OK]
Common Mistakes:
MISTAKES
  • Assuming inheritance from sealed class is allowed
  • Expecting runtime error instead of compile-time
  • Thinking sealed affects only methods, not classes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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