0
0
C++programming~20 mins

Pure virtual functions in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Pure Virtual Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a class with pure virtual function
What is the output of this C++ program?
C++
#include <iostream>
using namespace std;

class Shape {
public:
    virtual void draw() = 0;
};

class Circle : public Shape {
public:
    void draw() override {
        cout << "Drawing Circle" << endl;
    }
};

int main() {
    Circle c;
    c.draw();
    return 0;
}
AShape::draw called
BDrawing Circle
CCompilation error: cannot instantiate abstract class
DNo output
Attempts:
2 left
πŸ’‘ Hint
Look at which class implements the pure virtual function and which object is created.
❓ Predict Output
intermediate
2:00remaining
Output when calling pure virtual function from base pointer
What will be the output of this program?
C++
#include <iostream>
using namespace std;

class Animal {
public:
    virtual void sound() = 0;
};

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

int main() {
    Animal* a = new Dog();
    a->sound();
    delete a;
    return 0;
}
ARuntime error: pure virtual function call
BCompilation error: cannot instantiate abstract class
CWoof
DNo output
Attempts:
2 left
πŸ’‘ Hint
Check which class implements the pure virtual function and how the pointer is used.
πŸ”§ Debug
advanced
2:00remaining
Identify the error in this pure virtual destructor example
What error will this code produce when compiled?
C++
#include <iostream>
using namespace std;

class Base {
public:
    virtual ~Base() = 0;
};

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

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

int main() {
    Base* b = new Derived();
    delete b;
    return 0;
}
A
Output:
Derived destructor
Base destructor
BRuntime error: pure virtual function call
CCompilation error: pure virtual destructor must have a definition
DNo output
Attempts:
2 left
πŸ’‘ Hint
Pure virtual destructors must have a body to allow proper cleanup.
πŸ“ Syntax
advanced
2:00remaining
Which option correctly declares a pure virtual function?
Which of the following is the correct syntax to declare a pure virtual function named `foo` returning int?
Aint virtual foo() = 0;
Bint foo() = 0 virtual;
Cvirtual int foo() {} = 0;
Dvirtual int foo() = 0;
Attempts:
2 left
πŸ’‘ Hint
Remember the order: 'virtual' keyword before return type, and '= 0;' at the end.
πŸš€ Application
expert
2:00remaining
Number of objects instantiated with pure virtual functions
Given the following code, how many objects of type `Base` or its derived classes are instantiated in `main`?
C++
#include <iostream>
using namespace std;

class Base {
public:
    virtual void display() = 0;
};

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

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

int main() {
    Derived1 d1;
    Base* b = new Derived2();
    d1.display();
    b->display();
    delete b;
    return 0;
}
A2
B1
C3
D0
Attempts:
2 left
πŸ’‘ Hint
Count all objects created, including those created with new.