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

Runtime polymorphism execution in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Runtime Polymorphism Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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();
    }
}
ADog barks
BAnimal speaks
CCompile-time error
DRuntime exception
Attempts:
2 left
💡 Hint
Remember that virtual methods allow the derived class to provide its own implementation that is called at runtime.
Predict Output
intermediate
2: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();
    }
}
ADerived Show
BRuntime exception
CCompile-time error
DBase Show
Attempts:
2 left
💡 Hint
The new keyword hides the base method but does not override it.
🔧 Debug
advanced
2: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();
    }
}
AInvalidOperationException with message "Cat cannot speak now"
BNullReferenceException
CNo error, prints "Animal speaks"
DCompile-time error
Attempts:
2 left
💡 Hint
Check what the overridden method in the derived class does.
🧠 Conceptual
advanced
2: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();
    }
}
ADerived2 speaks
BDerived1 speaks
CCompile-time error
DBase speaks
Attempts:
2 left
💡 Hint
Check what the sealed keyword does to method overriding.
Predict Output
expert
2: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();
    }
}
ACar started
B
Vehicle started
Car started
CVehicle started
DCompile-time error
Attempts:
2 left
💡 Hint
The derived method calls the base method before printing its own message.