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

Why polymorphism matters in C Sharp (C#) - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Polymorphism Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of polymorphic method call
What is the output of this C# code demonstrating polymorphism?
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 Cat : Animal {
    public override void Speak() {
        Console.WriteLine("Cat meows");
    }
}

class Program {
    static void Main() {
        Animal myAnimal = new Dog();
        myAnimal.Speak();
    }
}
ADog barks
BAnimal speaks
CCat meows
DCompilation error
Attempts:
2 left
💡 Hint
Look at which class instance is assigned to the Animal variable and which Speak method is called.
🧠 Conceptual
intermediate
1:30remaining
Why use polymorphism?
Which of the following best explains why polymorphism is important in programming?
AIt forces all classes to have the same methods with identical implementations.
BIt makes programs run faster by avoiding method calls.
CIt prevents inheritance between classes to avoid confusion.
DIt allows objects of different types to be treated as instances of a common base type, enabling flexible and reusable code.
Attempts:
2 left
💡 Hint
Think about how polymorphism helps when you want to write code that works with many types of objects.
🔧 Debug
advanced
2:00remaining
Identify the runtime behavior with polymorphism
What will happen when this C# program runs?
C Sharp (C#)
using System;

class Shape {
    public virtual void Draw() {
        Console.WriteLine("Drawing a shape");
    }
}

class Circle : Shape {
    public new void Draw() {
        Console.WriteLine("Drawing a circle");
    }
}

class Program {
    static void Main() {
        Shape s = new Circle();
        s.Draw();
    }
}
ACompilation error due to 'new' keyword
BDrawing a circle
CDrawing a shape
DRuntime error
Attempts:
2 left
💡 Hint
Consider the difference between 'override' and 'new' keywords in method declarations.
📝 Syntax
advanced
2:00remaining
Which code snippet correctly implements polymorphism?
Select the code snippet that correctly uses polymorphism with method overriding in C#.
Aclass Base { public virtual void Show() { } } class Derived : Base { public override void Show() { } }
Bclass Base { public virtual void Show() { } } class Derived : Base { public new void Show() { } }
Cclass Base { public void Show() { } } class Derived : Base { public void Show() { } }
Dabstract class Base { public abstract void Show(); } class Derived : Base { public override void Show() { } }
Attempts:
2 left
💡 Hint
Look for the correct use of 'virtual' and 'override' keywords for polymorphism.
🚀 Application
expert
2:30remaining
Polymorphism in a collection of objects
Given the following code, what will be the output when the program runs?
C Sharp (C#)
using System;
using System.Collections.Generic;

abstract class Vehicle {
    public abstract void Start();
}

class Car : Vehicle {
    public override void Start() {
        Console.WriteLine("Car started");
    }
}

class Motorcycle : Vehicle {
    public override void Start() {
        Console.WriteLine("Motorcycle started");
    }
}

class Program {
    static void Main() {
        List<Vehicle> vehicles = new List<Vehicle> { new Car(), new Motorcycle() };
        foreach (var v in vehicles) {
            v.Start();
        }
    }
}
AVehicle started\nVehicle started
BCar started\nMotorcycle started
CCompilation error due to abstract class instantiation
DRuntime error due to missing method implementation
Attempts:
2 left
💡 Hint
Check how abstract methods are implemented and called on derived class instances in a list.