Bird
Raised Fist0
C Sharp (C#)programming~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main benefit of polymorphism in C# programming?
easy
A. It forces all classes to have the same properties.
B. It makes the program run faster by using less memory.
C. It prevents any changes to the code once compiled.
D. It allows one method to work with different types of objects.

Solution

  1. Step 1: Understand polymorphism concept

    Polymorphism means one method name can work with different object types.
  2. Step 2: Identify the main benefit

    This makes code easier to write and maintain by reusing method names for different classes.
  3. Final Answer:

    It allows one method to work with different types of objects. -> Option D
  4. Quick Check:

    Polymorphism = One method, many types [OK]
Hint: Polymorphism means one method, many object types [OK]
Common Mistakes:
  • Thinking polymorphism speeds up code automatically
  • Confusing polymorphism with code immutability
  • Believing polymorphism forces identical class properties
2. Which of the following is the correct way to declare a method that demonstrates polymorphism in C#?
easy
A. public void Draw() { }
B. public void Draw(int x) { }
C. public virtual void Draw() { }
D. public static void Draw() { }

Solution

  1. Step 1: Identify polymorphic method declaration

    In C#, polymorphism is often shown using virtual methods that can be overridden.
  2. Step 2: Check method options

    Only 'public virtual void Draw()' allows derived classes to override and show polymorphism.
  3. Final Answer:

    public virtual void Draw() { } -> Option C
  4. Quick Check:

    Virtual method enables polymorphism [OK]
Hint: Use 'virtual' keyword to enable polymorphism [OK]
Common Mistakes:
  • Choosing static methods which cannot be overridden
  • Ignoring the virtual keyword for polymorphism
  • Confusing method overloading with polymorphism
3. Consider the following C# code:
class Animal { public virtual string Speak() => "..."; }
class Dog : Animal { public override string Speak() => "Woof"; }
class Cat : Animal { public override string Speak() => "Meow"; }

Animal a = new Dog();
Console.WriteLine(a.Speak());

What is the output?
medium
A. Woof
B. ...
C. Meow
D. Compilation error

Solution

  1. Step 1: Understand polymorphism with virtual and override

    The variable 'a' is of type Animal but holds a Dog object. The Speak method is virtual and overridden in Dog.
  2. Step 2: Determine which Speak method runs

    At runtime, the Dog's Speak method runs, returning "Woof".
  3. Final Answer:

    Woof -> Option A
  4. Quick Check:

    Virtual method calls overridden version [OK]
Hint: Virtual method calls override in actual object type [OK]
Common Mistakes:
  • Expecting base class method output
  • Confusing variable type with object type
  • Thinking code causes compile error
4. What is wrong with this C# code snippet that tries to use polymorphism?
class Shape { public void Draw() { Console.WriteLine("Shape"); } }
class Circle : Shape { public void Draw() { Console.WriteLine("Circle"); } }

Shape s = new Circle();
s.Draw();
medium
A. Shape cannot be assigned a Circle object.
B. Draw method in Shape should be virtual to enable polymorphism.
C. Circle class must not have a Draw method.
D. Draw method must be static.

Solution

  1. Step 1: Check method declarations for polymorphism

    Polymorphism requires the base method to be marked 'virtual' and the derived method to 'override'.
  2. Step 2: Identify missing virtual keyword

    Here, Shape's Draw is not virtual, so Circle's Draw hides it but does not override.
  3. Final Answer:

    Draw method in Shape should be virtual to enable polymorphism. -> Option B
  4. Quick Check:

    Base method must be virtual for polymorphism [OK]
Hint: Base method needs 'virtual' for polymorphism [OK]
Common Mistakes:
  • Thinking method hiding equals polymorphism
  • Believing derived method must be removed
  • Assuming static methods support polymorphism
5. You have a list of different shapes (Circle, Square, Triangle) all inheriting from Shape with a virtual method Draw(). How does polymorphism help you when you want to draw all shapes without checking their types?
hard
A. You can call Draw() on each Shape reference and the correct shape's Draw runs automatically.
B. You must check each shape's type and call its specific Draw method manually.
C. You need to cast each shape to its exact type before calling Draw().
D. You cannot use polymorphism with collections of different shapes.

Solution

  1. Step 1: Understand polymorphism with collections

    Polymorphism allows calling the same method on base class references that point to different derived objects.
  2. Step 2: Apply to drawing shapes

    Calling Draw() on each Shape in the list runs the correct overridden Draw method for each shape automatically.
  3. Final Answer:

    You can call Draw() on each Shape reference and the correct shape's Draw runs automatically. -> Option A
  4. Quick Check:

    Polymorphism enables method calls without type checks [OK]
Hint: Call base method; derived version runs automatically [OK]
Common Mistakes:
  • Thinking you must check types before calling methods
  • Trying to cast objects unnecessarily
  • Believing polymorphism doesn't work with lists