Bird
Raised Fist0
C++programming~20 mins

Runtime polymorphism in C++ - Practice Problems & Coding Challenges

Choose your learning style10 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 virtual function call through base pointer
What is the output of this C++ code when run?
C++
#include <iostream>
using namespace std;

class Animal {
public:
    virtual void sound() { cout << "Animal sound" << endl; }
};

class Dog : public Animal {
public:
    void sound() override { cout << "Bark" << endl; }
};

int main() {
    Animal* a = new Dog();
    a->sound();
    delete a;
    return 0;
}
AAnimal sound
BBark
CCompilation error
DRuntime error
Attempts:
2 left
πŸ’‘ Hint
Think about which version of the function is called when using a base class pointer to a derived object.
❓ Predict Output
intermediate
2:00remaining
Output of non-virtual function call through base pointer
What will this C++ program print when executed?
C++
#include <iostream>
using namespace std;

class Base {
public:
    void show() { cout << "Base show" << endl; }
};

class Derived : public Base {
public:
    void show() { cout << "Derived show" << endl; }
};

int main() {
    Base* b = new Derived();
    b->show();
    delete b;
    return 0;
}
ACompilation error
BDerived show
CBase show
DRuntime error
Attempts:
2 left
πŸ’‘ Hint
Is the function show() declared virtual? How does that affect which function is called?
πŸ”§ Debug
advanced
2:00remaining
Identify the runtime error in polymorphic code
This code compiles but causes a runtime error. What is the cause?
C++
#include <iostream>
using namespace std;

class Base {
public:
    virtual void display() { cout << "Base display" << endl; }
    ~Base() { cout << "Base destructor" << endl; }
};

class Derived : public Base {
public:
    void display() override { cout << "Derived display" << endl; }
    ~Derived() { cout << "Derived destructor" << endl; }
};

int main() {
    Base* b = new Derived();
    b->display();
    delete b;
    return 0;
}
ANo runtime error occurs
BVirtual function display() is not overridden correctly
CBase destructor is virtual causing double delete
DDerived destructor is not called causing resource leak
Attempts:
2 left
πŸ’‘ Hint
Check if the destructor in Base is virtual and how that affects deleting derived objects.
πŸ“ Syntax
advanced
2:00remaining
Identify the syntax error in polymorphic class declaration
Which option contains a syntax error in this polymorphic class code?
C++
class Shape {
public:
    virtual void draw() = 0;
};

class Circle : public Shape {
public:
    void draw() override { cout << "Circle" << endl; }
};
Aclass Triangle : public Shape { public: void draw() override; };
Bclass Circle : public Shape { public: void draw() override { cout << "Circle" << endl; } };
Cclass Square : public Shape { public: void draw() { cout << "Square" << endl; } };
Dclass Shape { public: virtual void draw() = 0; };
Attempts:
2 left
πŸ’‘ Hint
Check if the function declared with override has a body or is defined outside the class.
πŸš€ Application
expert
3:00remaining
Determine the number of objects created and destructed
Consider this C++ program using runtime polymorphism. How many times are constructors and destructors called in total?
C++
#include <iostream>
using namespace std;

class Base {
public:
    Base() { cout << "Base constructor" << endl; }
    virtual ~Base() { cout << "Base destructor" << endl; }
};

class Derived : public Base {
public:
    Derived() { cout << "Derived constructor" << endl; }
    ~Derived() { cout << "Derived destructor" << endl; }
};

int main() {
    Base* arr[3];
    arr[0] = new Base();
    arr[1] = new Derived();
    arr[2] = new Derived();

    for (int i = 0; i < 3; i++) {
        delete arr[i];
    }
    return 0;
}
A5 constructors and 5 destructors called
B6 constructors and 3 destructors called
C3 constructors and 3 destructors called
D6 constructors and 6 destructors called
Attempts:
2 left
πŸ’‘ Hint
Remember each Derived object calls Base constructor first. Also destructors are called in reverse order.

Practice

(1/5)
1. What is the main purpose of virtual functions in C++ runtime polymorphism?
easy
A. To allocate memory for objects dynamically
B. To make functions run faster by avoiding function calls
C. To prevent any function from being overridden in derived classes
D. To allow derived classes to override base class methods and decide at runtime which method to call

Solution

  1. Step 1: Understand virtual functions role

    Virtual functions allow a base class to declare a function that derived classes can override.
  2. Step 2: Explain runtime decision

    At runtime, the program decides which overridden function to call based on the actual object type.
  3. Final Answer:

    To allow derived classes to override base class methods and decide at runtime which method to call -> Option D
  4. Quick Check:

    Virtual functions enable runtime method selection [OK]
Hint: Virtual means choose function at runtime, not compile time [OK]
Common Mistakes:
  • Thinking virtual speeds up function calls
  • Believing virtual prevents overriding
  • Confusing virtual with memory allocation
2. Which of the following is the correct syntax to declare a virtual function in a base class?
easy
A. void display() virtual;
B. void virtual display();
C. virtual void display();
D. virtual: void display();

Solution

  1. Step 1: Recall virtual function syntax

    The keyword virtual comes before the return type in the function declaration.
  2. Step 2: Match correct syntax

    virtual void display(); correctly places virtual before void display();
  3. Final Answer:

    virtual void display(); -> Option C
  4. Quick Check:

    Virtual keyword precedes return type [OK]
Hint: Virtual always goes before return type in declaration [OK]
Common Mistakes:
  • Placing virtual after function name
  • Using virtual as a label or modifier incorrectly
  • Missing semicolon after declaration
3. What will be the output of the following code?
class Base {
public:
    virtual void show() { std::cout << "Base"; }
};
class Derived : public Base {
public:
    void show() override { std::cout << "Derived"; }
};
int main() {
    Base* ptr = new Derived();
    ptr->show();
    delete ptr;
    return 0;
}
medium
A. Derived
B. Base
C. Compilation error
D. Runtime error

Solution

  1. Step 1: Identify virtual function usage

    The show() function in Base is virtual and overridden in Derived.
  2. Step 2: Understand pointer type and object type

    The pointer is of type Base* but points to a Derived object, so the overridden show() in Derived is called at runtime.
  3. Final Answer:

    Derived -> Option A
  4. Quick Check:

    Virtual function calls derived version [OK]
Hint: Virtual functions call derived method via base pointer [OK]
Common Mistakes:
  • Expecting base class method output
  • Confusing pointer type with object type
  • Ignoring virtual keyword effect
4. Identify the error in the following code snippet related to runtime polymorphism:
class Animal {
public:
    void sound() { std::cout << "Animal sound"; }
};
class Dog : public Animal {
public:
    void sound() { std::cout << "Bark"; }
};
int main() {
    Animal* a = new Dog();
    a->sound();
    delete a;
    return 0;
}
medium
A. Incorrect pointer type declaration
B. Missing virtual keyword in base class function
C. Derived class function should be static
D. Memory leak due to missing delete

Solution

  1. Step 1: Check base class function declaration

    The base class function sound() is not declared virtual, so no runtime polymorphism occurs.
  2. Step 2: Effect on function call

    Because sound() is not virtual, calling a->sound() calls the base class version, not the derived one.
  3. Final Answer:

    Missing virtual keyword in base class function -> Option B
  4. Quick Check:

    Virtual needed for runtime polymorphism [OK]
Hint: Base function must be virtual for polymorphism [OK]
Common Mistakes:
  • Assuming override works without virtual
  • Thinking pointer type causes error
  • Ignoring virtual keyword importance
5. Given the classes below, what will be the output when ptr->identify() is called?
class Shape {
public:
    virtual void identify() { std::cout << "Shape"; }
};
class Circle : public Shape {
public:
    void identify() override { std::cout << "Circle"; }
};
class ColoredCircle : public Circle {
public:
    void identify() override { std::cout << "Colored Circle"; }
};
int main() {
    Shape* ptr = new ColoredCircle();
    ptr->identify();
    delete ptr;
    return 0;
}
hard
A. Colored Circle
B. Circle
C. Shape
D. Compilation error due to multiple overrides

Solution

  1. Step 1: Trace virtual function overrides

    identify() is virtual in Shape, overridden in Circle, and again in ColoredCircle.
  2. Step 2: Determine actual object type at runtime

    The pointer ptr is of type Shape* but points to a ColoredCircle object, so the most derived override runs.
  3. Final Answer:

    Colored Circle -> Option A
  4. Quick Check:

    Most derived override called at runtime [OK]
Hint: Runtime calls most derived override in inheritance chain [OK]
Common Mistakes:
  • Expecting base or intermediate class output
  • Thinking multiple overrides cause errors
  • Ignoring virtual override chain
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard