Bird
Raised Fist0
C++programming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a base class in C++?
easy
A. To store only private data members
B. To create objects directly without inheritance
C. To override functions in derived classes
D. To hold common features shared by derived classes

Solution

  1. Step 1: Understand base class role

    A base class is designed to hold common features that multiple derived classes can share.
  2. Step 2: Compare options

    To hold common features shared by derived classes correctly states the purpose. Other options describe derived class behavior or incorrect roles.
  3. Final Answer:

    To hold common features shared by derived classes -> Option D
  4. Quick Check:

    Base class = common features [OK]
Hint: Base class holds shared features for reuse [OK]
Common Mistakes:
  • Confusing base class with derived class
  • Thinking base class cannot have data members
  • Assuming base class is only for private data
2. Which of the following is the correct syntax to declare a derived class Car inheriting from base class Vehicle in C++?
easy
A. class Car : Vehicle {};
B. class Car : public Vehicle {};
C. class Car extends Vehicle {};
D. class Car inherits Vehicle {};

Solution

  1. Step 1: Recall C++ inheritance syntax

    In C++, a derived class inherits from a base class using a colon and access specifier like public.
  2. Step 2: Check options

    class Car : public Vehicle {}; uses correct syntax: class Car : public Vehicle {}. Others use incorrect keywords or omit access specifier.
  3. Final Answer:

    class Car : public Vehicle {} -> Option B
  4. Quick Check:

    Inheritance syntax = colon + access specifier [OK]
Hint: Use colon and public keyword for inheritance [OK]
Common Mistakes:
  • Omitting access specifier like public
  • Using wrong keywords like inherits or extends
  • Forgetting colon before base class name
3. What will be the output of this C++ code?
class Base {
public:
    void show() { std::cout << "Base"; }
};
class Derived : public Base {
public:
    void show() { std::cout << "Derived"; }
};
int main() {
    Derived d;
    d.show();
    return 0;
}
medium
A. Derived
B. Base
C. Compilation error
D. No output

Solution

  1. Step 1: Understand method overriding in derived class

    The derived class Derived has its own show() method which hides the base class method.
  2. Step 2: Check which method is called

    Calling d.show() on a Derived object calls the derived class method, printing "Derived".
  3. Final Answer:

    Derived -> Option A
  4. Quick Check:

    Derived method overrides base method [OK]
Hint: Derived class method hides base method if same name [OK]
Common Mistakes:
  • Assuming base class method runs without virtual keyword
  • Expecting compilation error due to method name clash
  • Thinking both outputs print
4. Identify the error in this code snippet:
class Base {
public:
    void display() { std::cout << "Base"; }
};
class Derived : public Base {
public:
    void display() { std::cout << "Derived"; }
};
int main() {
    Base* ptr = new Derived();
    ptr->display();
    return 0;
}
medium
A. Missing virtual keyword in base class method
B. Incorrect pointer type for inheritance
C. Derived class method should be static
D. No error, code runs fine

Solution

  1. Step 1: Understand polymorphism with base pointers

    Calling display() via a base class pointer calls base method unless it is virtual.
  2. Step 2: Check for virtual keyword

    The base class method display() lacks virtual, so ptr->display() calls base version, not derived.
  3. Final Answer:

    Missing virtual keyword in base class method -> Option A
  4. Quick Check:

    Virtual needed for polymorphic calls [OK]
Hint: Add virtual to base method for polymorphism [OK]
Common Mistakes:
  • Thinking pointer type is wrong
  • Believing derived method runs without virtual
  • Assuming static keyword fixes this
5. Given these classes:
class Animal {
public:
    virtual std::string sound() { return "Unknown"; }
};
class Dog : public Animal {
public:
    std::string sound() override { return "Bark"; }
};
class Cat : public Animal {
public:
    std::string sound() override { return "Meow"; }
};

Which code snippet correctly creates a list of animals and prints their sounds?
hard
A. std::vector<Animal> animals = {Dog(), Cat()}; for(auto& a : animals) std::cout << a.sound() << ' ';
B. std::vector<Dog> animals = {Dog(), Cat()}; for(auto& a : animals) std::cout << a.sound() << ' ';
C. std::vector animals = {new Dog(), new Cat()}; for(auto a : animals) std::cout << a->sound() << ' ';
D. std::vector<Animal> animals = {Dog(), Cat()}; for(auto a : animals) std::cout << a.sound() << ' ';

Solution

  1. Step 1: Understand polymorphism with collections

    To preserve derived class behavior, store pointers to base class, not objects by value.
  2. Step 2: Analyze options

    std::vector animals = {new Dog(), new Cat()}; for(auto a : animals) std::cout << a->sound() << ' '; uses std::vector<Animal*> with pointers to Dog and Cat, calling sound() polymorphically.
  3. Final Answer:

    std::vector<Animal*> animals = {new Dog(), new Cat()}; for(auto a : animals) std::cout << a->sound() << ' '; -> Option C
  4. Quick Check:

    Use base class pointers for polymorphic containers [OK]
Hint: Use base class pointers to keep derived behavior [OK]
Common Mistakes:
  • Storing objects by value causes slicing
  • Using wrong container type for polymorphism
  • Forgetting to use pointers or references
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