0
0
C++programming~20 mins

Virtual functions in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Virtual Functions Mastery
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?
C++
#include <iostream>
using namespace std;

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

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

int main() {
    Base* ptr = new Derived();
    ptr->show();
    delete ptr;
    return 0;
}
ACompilation error
BBase show
CRuntime error
DDerived show
Attempts:
2 left
πŸ’‘ Hint
Remember how virtual functions behave when called through a base class pointer.
❓ Predict Output
intermediate
2:00remaining
Output when virtual function is not overridden
What will this program print?
C++
#include <iostream>
using namespace std;

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

class Dog : public Animal {
    // No override of speak()
};

int main() {
    Animal* a = new Dog();
    a->speak();
    delete a;
    return 0;
}
AAnimal speaks
BCompilation error
CDog speaks
DRuntime error
Attempts:
2 left
πŸ’‘ Hint
If a derived class does not override a virtual function, which version is called?
πŸ”§ Debug
advanced
2:00remaining
Identify the error in virtual destructor usage
What error will this code produce when compiled or run?
C++
#include <iostream>
using namespace std;

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

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

int main() {
    Base* ptr = new Derived();
    delete ptr;
    return 0;
}
ABoth Derived and Base destructors are called
BOnly Base destructor is called, Derived destructor is skipped
CCompilation error due to missing virtual keyword
DRuntime crash due to invalid delete
Attempts:
2 left
πŸ’‘ Hint
Think about what happens if the base class destructor is not virtual and you delete a derived object through a base pointer.
πŸ“ Syntax
advanced
2:00remaining
Which option causes a compilation error?
Given the following class, which option will cause a compilation error?
C++
class Shape {
public:
    virtual void draw() = 0;
};
Aclass Square : public Shape {};
Bclass Rectangle : public Shape { void draw() override { /* code */ } };
Cclass Triangle : public Shape { void draw() override; };
Dclass Circle : public Shape { void draw() override { /* code */ } };
Attempts:
2 left
πŸ’‘ Hint
What happens if a class inherits a pure virtual function but does not override it?
πŸš€ Application
expert
2:00remaining
Determine the number of virtual function calls
How many times is the virtual function 'action' called in this program?
C++
#include <iostream>
using namespace std;

class A {
public:
    virtual void action() { cout << "A"; }
};

class B : public A {
public:
    void action() override { cout << "B"; }
};

class C : public B {
public:
    void action() override { cout << "C"; }
};

int main() {
    A* arr[3];
    arr[0] = new A();
    arr[1] = new B();
    arr[2] = new C();

    for (int i = 0; i < 3; ++i) {
        arr[i]->action();
    }

    for (int i = 0; i < 3; ++i) {
        delete arr[i];
    }
    return 0;
}
A6
B1
C3
D0
Attempts:
2 left
πŸ’‘ Hint
Count how many times the loop calls the virtual function.