AlgoMaster Logo

Polymorphism - Quiz

Last Updated: January 3, 2026

1 min read

Polymorphism Quiz

29 quizzes

1
Multiple Choice

Which statement best describes runtime polymorphism in C++?

2
Multiple Choice

Which of the following is required to achieve runtime polymorphism?

3
Multiple Choice

What happens if a derived class does NOT override a virtual function from the base class?

4
Multiple Choice

What is a correct reason to use a pure virtual function?

5
Multiple Choice

Which class is abstract?

6
Multiple Choice

Why should a polymorphic base class usually have a virtual destructor?

7
Multiple Choice

Which is true about RTTI (Runtime Type Information) in C++?

8
Multiple Choice

What does dynamic_cast do when downcasting a pointer and the cast is invalid?

9
Multiple Choice

Which declaration correctly defines a pure virtual function?

10
Multiple Choice

How does using virtual functions typically affect performance?

11
Multiple Choice

Which cast should you prefer for safe polymorphic downcasting?

12
Code Completion

Declare a virtual function to enable polymorphic drawing of widgets

cpp
1
class Widget {
2
public:
3
void draw() const;
4
};

Click an option to fill the blank:

13
Code Completion

Declare a pure virtual function for computing the area of a shape

cpp
1
class Shape {
2
public:
3
virtual double area() const ;
4
};

Click an option to fill the blank:

14
Code Completion

Use dynamic_cast to safely downcast a base pointer to Derived*

cpp
1
Derived* d = <Derived*>(basePtr);

Click an option to fill the blank:

15
Output Prediction

What is the output of this code?

1#include <iostream>
2
3class Base {
4public:
5    virtual void f() const { std::cout << "Base::f\n"; }
6};
7
8class Derived : public Base {
9public:
10    void f() const override { std::cout << "Derived::f\n"; }
11};
12
13int main() {
14    Derived d;
15    Base* b = &d;
16    b->f();
17    return 0;
18}
16
Output Prediction

What is the output of this code using an abstract base pointer?

1#include <iostream>
2
3class Shape {
4public:
5    virtual void draw() const = 0;
6    virtual ~Shape() { std::cout << "Shape dtor\n"; }
7};
8
9class Circle : public Shape {
10public:
11    void draw() const override { std::cout << "Circle::draw\n"; }
12    ~Circle() { std::cout << "Circle dtor\n"; }
13};
14
15int main() {
16    Shape* s = new Circle();
17    s->draw();
18    delete s;
19    return 0;
20}
17
Output Prediction

What is the output when calling a non-overridden virtual function?

1#include <iostream>
2
3class Logger {
4public:
5    virtual void info() const { std::cout << "Logger::info\n"; }
6    virtual void error() const { std::cout << "Logger::error\n"; }
7};
8
9class FileLogger : public Logger {
10public:
11    void error() const override { std::cout << "FileLogger::error\n"; }
12};
13
14int main() {
15    FileLogger fl;
16    Logger& ref = fl;
17    ref.info();
18    ref.error();
19    return 0;
20}
18
Output Prediction

What is the output of this RTTI example?

1#include <iostream>
2#include <typeinfo>
3
4class Base {
5public:
6    virtual ~Base() {}
7};
8
9class Derived : public Base {};
10
11int main() {
12    Base* b1 = new Base;
13    Base* b2 = new Derived;
14
15    std::cout << std::boolalpha;
16    std::cout << (dynamic_cast<Derived*>(b1) != nullptr) << "\n";
17    std::cout << (dynamic_cast<Derived*>(b2) != nullptr) << "\n";
18
19    delete b1;
20    delete b2;
21    return 0;
22}
19
Output Prediction

What is the output when slicing occurs but calling through a reference?

1#include <iostream>
2
3class Animal {
4public:
5    virtual void speak() const { std::cout << "Animal\n"; }
6};
7
8class Dog : public Animal {
9public:
10    void speak() const override { std::cout << "Dog\n"; }
11};
12
13void makeSpeak(Animal a) {
14    a.speak();
15}
16
17int main() {
18    Dog d;
19    Animal& ref = d;
20    ref.speak();
21    makeSpeak(d);
22    return 0;
23}
20
Bug Spotting

Find the bug related to polymorphic deletion

Click on the line(s) that contain the bug.

cpp
1
#include <iostream>
2
 
3
class Base {
4
public:
5
    Base() { std::cout << "Base ctor\n"; }
6
    ~Base() { std::cout << "Base dtor\n"; }
7
};
8
 
9
class Derived : public Base {
10
public:
11
    Derived() { std::cout << "Derived ctor\n"; }
12
    ~Derived() { std::cout << "Derived dtor\n"; }
13
};
14
 
15
int main() {
16
    Base* b = new Derived();
17
    delete b;
18
    return 0;
19
}
21
Bug Spotting

Find the bug in this RTTI-based downcast

Click on the line(s) that contain the bug.

cpp
1
#include <iostream>
2
 
3
class Shape {
4
public:
5
    void draw() const { std::cout << "Shape::draw\n"; }
6
};
7
 
8
class Circle : public Shape {
9
public:
10
    void draw() const { std::cout << "Circle::draw\n"; }
11
};
12
 
13
int main() {
14
    Shape* s = new Circle();
15
    Circle* c = dynamic_cast<Circle*>(s);
16
    if (c) c->draw();
17
    delete s;
18
    return 0;
19
}
22
Hotspot Selection

Click the line where dynamic dispatch actually occurs

Click on the line to select.

cpp
1
#include <memory>
2
#include <iostream>
3
 
4
class Command {
5
public:
6
    virtual void execute() = 0;
7
    virtual ~Command() = default;
8
};
9
 
10
class PrintCommand : public Command {
11
public:
12
    void execute() override { std::cout << "Print\n"; }
13
};
14
 
15
int main() {
16
    std::unique_ptr<Command> cmd = std::make_unique<PrintCommand>();
17
    cmd->execute();
18
    return 0;
19
}
23
Hotspot Selection

Click the line that makes Shape an abstract class

Click on the line to select.

cpp
1
#include <iostream>
2
 
3
class Shape {
4
public:
5
    virtual void draw() const = 0;
6
    virtual ~Shape() = default;
7
};
8
 
9
class Rectangle : public Shape {
10
public:
11
    void draw() const override { std::cout << "Rectangle\n"; }
12
};
13
 
14
int main() {
15
    // Shape s; // not allowed
16
    Rectangle r;
17
    r.draw();
18
    return 0;
19
}
24
Fill in the Blanks

Complete the code to use dynamic_cast safely in an event handling system

cpp
1
class Event { public: virtual ~Event() = default; };
2
class MouseEvent : public Event { };
3
4
void handle(Event* e) {
5
if (auto* m = <MouseEvent*>()) {
6
// handle mouse event
7
}
8
}

Click an option to fill blank 1:

25
Fill in the Blanks

Complete the interface to support cloning via polymorphism

cpp
1
class Shape {
2
public:
3
virtual ~Shape() = default;
4
virtual Shape* () const = 0;
5
};
6
7
class Circle : public Shape {
8
public:
9
Circle* () const override { return new Circle(*this); }
10
};

Click an option to fill blank 1:

26
Matching

Match the polymorphism-related C++ keyword with its primary purpose

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

27
Matching

Match the class kind with its polymorphism role

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

28
Sequencing

Order the steps when calling a virtual function through a base pointer at runtime

Drag and drop to reorder, or use the arrows.

29
Sequencing

Order the steps to define and use an abstract base class for polymorphic drawing

Drag and drop to reorder, or use the arrows.