0
0
C Sharp (C#)programming~5 mins

Runtime polymorphism execution in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is runtime polymorphism in C#?
Runtime polymorphism means that the method to be called is determined during program execution, not at compile time. It allows a base class reference to call derived class methods using method overriding.
Click to reveal answer
beginner
How do you enable runtime polymorphism in C#?
By declaring a method in the base class as <code>virtual</code> and overriding it in the derived class using <code>override</code>. Then, calling the method through a base class reference triggers the derived class version at runtime.
Click to reveal answer
beginner
What keyword is used in the base class method to allow overriding?
The <code>virtual</code> keyword marks a method in the base class as overridable, enabling runtime polymorphism.
Click to reveal answer
intermediate
What happens if you call a non-virtual method through a base class reference?
The base class method is called, even if the derived class has a method with the same name. This is because non-virtual methods are resolved at compile time, not runtime.
Click to reveal answer
beginner
Explain the role of the override keyword in runtime polymorphism.
The <code>override</code> keyword tells the compiler that the derived class method replaces the base class virtual method. This enables the runtime to call the correct method version based on the actual object type.
Click to reveal answer
Which keyword in C# allows a method to be overridden for runtime polymorphism?
Avirtual
Bstatic
Csealed
Dconst
What keyword must a derived class method use to override a base class virtual method?
Anew
Boverride
Cvirtual
Dabstract
If a method is not marked virtual, what happens when called through a base class reference?
ADerived class method is called
BCompile error occurs
CRuntime error occurs
DBase class method is called
What determines which method is called in runtime polymorphism?
ARuntime type of the object
BCompile-time type of the reference
CMethod name length
DNumber of parameters
Which of these is NOT required for runtime polymorphism in C#?
ABase class method marked virtual
BCalling method through base class reference
CMethod marked as static
DDerived class method marked override
Describe how runtime polymorphism works in C# with an example.
Think about how a base class pointer can call a method that behaves differently depending on the actual object.
You got /4 concepts.
    Explain the difference between virtual and non-virtual methods in the context of runtime polymorphism.
    Consider what happens when you call a method through a base class reference.
    You got /4 concepts.