Challenge - 5 Problems
Runtime Polymorphism Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of overridden method call
What is the output of this C# program when calling the
Speak method on a base class reference pointing to a derived class object?C Sharp (C#)
using System; class Animal { public virtual void Speak() { Console.WriteLine("Animal speaks"); } } class Dog : Animal { public override void Speak() { Console.WriteLine("Dog barks"); } } class Program { static void Main() { Animal myAnimal = new Dog(); myAnimal.Speak(); } }
Attempts:
2 left
💡 Hint
Remember that virtual methods allow the derived class to provide its own implementation that is called at runtime.
✗ Incorrect
The
Speak method is marked as virtual in the base class and overridden in the derived class. When calling Speak on a base class reference that points to a derived class object, the derived class's method runs, printing "Dog barks".❓ Predict Output
intermediate2:00remaining
Output of method hiding vs overriding
What will be printed when running this C# code?
C Sharp (C#)
using System; class Base { public void Show() { Console.WriteLine("Base Show"); } } class Derived : Base { public new void Show() { Console.WriteLine("Derived Show"); } } class Program { static void Main() { Base obj = new Derived(); obj.Show(); } }
Attempts:
2 left
💡 Hint
The
new keyword hides the base method but does not override it.✗ Incorrect
Because
Show in Derived hides the base method instead of overriding it, calling Show on a base class reference calls the base class method, printing "Base Show".🔧 Debug
advanced2:00remaining
Identify runtime error in polymorphic call
What runtime error will this C# code produce when executed?
C Sharp (C#)
using System; class Animal { public virtual void Speak() { Console.WriteLine("Animal speaks"); } } class Cat : Animal { public override void Speak() { throw new InvalidOperationException("Cat cannot speak now"); } } class Program { static void Main() { Animal pet = new Cat(); pet.Speak(); } }
Attempts:
2 left
💡 Hint
Check what the overridden method in the derived class does.
✗ Incorrect
The
Speak method in Cat throws an InvalidOperationException. Since the base class reference points to a Cat object, calling Speak runs the overridden method and throws the exception.🧠 Conceptual
advanced2:00remaining
Effect of sealed override on runtime polymorphism
Consider this C# code snippet. What will be the output when calling
Speak on a Base reference to a Derived2 object?C Sharp (C#)
using System; class Base { public virtual void Speak() { Console.WriteLine("Base speaks"); } } class Derived1 : Base { public sealed override void Speak() { Console.WriteLine("Derived1 speaks"); } } class Derived2 : Derived1 { public override void Speak() { Console.WriteLine("Derived2 speaks"); } } class Program { static void Main() { Base obj = new Derived2(); obj.Speak(); } }
Attempts:
2 left
💡 Hint
Check what the sealed keyword does to method overriding.
✗ Incorrect
The
Speak method in Derived1 is sealed, so Derived2 cannot override it. Attempting to override causes a compile-time error.❓ Predict Output
expert2:00remaining
Output of polymorphic method with base call
What is the output of this C# program?
C Sharp (C#)
using System; class Vehicle { public virtual void Start() { Console.WriteLine("Vehicle started"); } } class Car : Vehicle { public override void Start() { base.Start(); Console.WriteLine("Car started"); } } class Program { static void Main() { Vehicle v = new Car(); v.Start(); } }
Attempts:
2 left
💡 Hint
The derived method calls the base method before printing its own message.
✗ Incorrect
The
Start method in Car calls base.Start() which prints "Vehicle started", then prints "Car started". So both lines appear in order.