Bird
0
0

What will happen if you try to instantiate an abstract class directly in C#?

medium📝 Predict Output Q5 of 15
C Sharp (C#) - Polymorphism and Abstract Classes
What will happen if you try to instantiate an abstract class directly in C#?
abstract class Animal { }
class Program {
    static void Main() {
        Animal a = new Animal();
    }
}
AWarning but program runs
BCompilation error: Cannot create an instance of the abstract class
CProgram runs and creates an Animal object
DRuntime error: NullReferenceException
Step-by-Step Solution
Solution:
  1. Step 1: Recall abstract class instantiation rules

    Abstract classes cannot be instantiated directly in C#. Attempting to do so causes a compile-time error.
  2. Step 2: Analyze the code behavior

    The code tries to create an Animal object directly, which is abstract, so compiler will reject it.
  3. Final Answer:

    Compilation error: Cannot create an instance of the abstract class -> Option B
  4. Quick Check:

    Abstract class instantiation = Compilation error [OK]
Quick Trick: Abstract classes cannot be instantiated directly [OK]
Common Mistakes:
MISTAKES
  • Expecting runtime error instead of compile error
  • Confusing abstract with concrete class instantiation
  • Ignoring compiler error messages

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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