0
0
C++programming~20 mins

Base and derived classes in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Master of Base and Derived Classes
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of virtual function call in base pointer

What is the output of this C++ code?

C++
#include <iostream>
using namespace std;

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

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

int main() {
    Base* b = new Derived();
    b->show();
    delete b;
    return 0;
}
ADerived
BBase
CCompilation error
DRuntime error
Attempts:
2 left
πŸ’‘ Hint

Think about how virtual functions work with base class pointers.

❓ Predict Output
intermediate
2:00remaining
Output of constructor and destructor calls

What is the output of this C++ program?

C++
#include <iostream>
using namespace std;

class Base {
public:
    Base() { cout << "Base constructor\n"; }
    ~Base() { cout << "Base destructor\n"; }
};

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

int main() {
    Base* b = new Derived();
    delete b;
    return 0;
}
A
Derived constructor
Base constructor
Derived destructor
Base destructor
B
Base constructor
Derived constructor
Derived destructor
Base destructor
C
Base constructor
Derived constructor
Base destructor
DCompilation error
Attempts:
2 left
πŸ’‘ Hint

Check if destructors are virtual or not.

πŸ”§ Debug
advanced
2:00remaining
Identify the error in derived class constructor initialization

What error does this code produce?

C++
#include <iostream>
using namespace std;

class Base {
    int x;
public:
    Base(int val) : x(val) {}
};

class Derived : public Base {
    int y;
public:
    Derived(int val) { y = val; }
};

int main() {
    Derived d(5);
    return 0;
}
ACompilation error: missing semicolon
BNo error, runs fine
CRuntime error: uninitialized base class
DCompilation error: Base class constructor not called
Attempts:
2 left
πŸ’‘ Hint

Check how base class constructors are called in derived classes.

πŸ“ Syntax
advanced
2:00remaining
Which option correctly overrides a base class method?

Given the base class below, which derived class method correctly overrides void display() const?

C++
class Base {
public:
    virtual void display() const;
};
Avoid display();
Bvoid display() const override;
Cvoid display() const & override;
Dvoid display() override;
Attempts:
2 left
πŸ’‘ Hint

Check the method signature including const qualifier and override keyword.

πŸš€ Application
expert
3:00remaining
Number of objects created and destroyed

Consider the following code. How many times are constructors and destructors called in total?

C++
#include <iostream>
using namespace std;

class Base {
public:
    Base() { cout << "Base constructed\n"; }
    virtual ~Base() { cout << "Base destroyed\n"; }
};

class Derived : public Base {
public:
    Derived() { cout << "Derived constructed\n"; }
    ~Derived() { cout << "Derived destroyed\n"; }
};

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;
}
A6 constructors and 6 destructors
B5 constructors and 5 destructors
C6 constructors and 5 destructors
D5 constructors and 6 destructors
Attempts:
2 left
πŸ’‘ Hint

Count each object creation and destruction carefully, including base and derived parts.