Challenge - 5 Problems
Runtime Polymorphism Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2: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; }
Attempts:
2 left
π‘ Hint
Think about which version of the function is called when using a base class pointer to a derived object.
β Incorrect
Because the function sound() is virtual, the call a->sound() uses the derived class Dog's override, printing "Bark".
β Predict Output
intermediate2: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; }
Attempts:
2 left
π‘ Hint
Is the function show() declared virtual? How does that affect which function is called?
β Incorrect
Since show() is not virtual, the call b->show() calls Base's version, printing "Base show".
π§ Debug
advanced2: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; }
Attempts:
2 left
π‘ Hint
Check if the destructor in Base is virtual and how that affects deleting derived objects.
β Incorrect
Base destructor is not virtual, so deleting Base* pointing to Derived object calls only Base destructor, skipping Derived destructor and causing resource leaks.
π Syntax
advanced2: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; } };
Attempts:
2 left
π‘ Hint
Check if the function declared with override has a body or is defined outside the class.
β Incorrect
Option A declares draw() with override but does not provide a function body or definition, causing a linker error (not a syntax error strictly, but incomplete definition).
π Application
expert3: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; }
Attempts:
2 left
π‘ Hint
Remember each Derived object calls Base constructor first. Also destructors are called in reverse order.
β Incorrect
Each Derived object calls Base constructor then Derived constructor (2 constructors per Derived). There are 2 Derived and 1 Base objects: total 3 Base constructors + 2 Derived constructors = 5 constructors. Destructors called similarly: Derived destructors for 2 Derived objects + Base destructors for all 3 objects = 5 destructors total. So total constructors and destructors are 5 each.