Bird
Raised Fist0
C++programming~20 mins

Types of inheritance in C++ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Inheritance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Single Inheritance Example
What is the output of this C++ code demonstrating single inheritance?
C++
#include <iostream>
using namespace std;

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

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

int main() {
    Dog d;
    d.sound();
    return 0;
}
ADog barks\nAnimal sound
BDog barks
CCompilation error
DAnimal sound
Attempts:
2 left
πŸ’‘ Hint
Look at which class's sound() method is called by the Dog object.
❓ Predict Output
intermediate
2:00remaining
Output of Multiple Inheritance Example
What is the output of this C++ code demonstrating multiple inheritance?
C++
#include <iostream>
using namespace std;

class Printer {
public:
    void print() { cout << "Printing document" << endl; }
};

class Scanner {
public:
    void scan() { cout << "Scanning document" << endl; }
};

class Copier : public Printer, public Scanner {
};

int main() {
    Copier c;
    c.print();
    c.scan();
    return 0;
}
AScanning document
BCompilation error due to ambiguity
CPrinting document
DPrinting document\nScanning document
Attempts:
2 left
πŸ’‘ Hint
Copier inherits both Printer and Scanner, so it can call both methods.
❓ Predict Output
advanced
2:00remaining
Output of Multilevel Inheritance with Overriding
What is the output of this C++ code demonstrating multilevel inheritance and method overriding?
C++
#include <iostream>
using namespace std;

class Vehicle {
public:
    virtual void start() { cout << "Vehicle starts" << endl; }
};

class Car : public Vehicle {
public:
    void start() override { cout << "Car starts" << endl; }
};

class SportsCar : public Car {
public:
    void start() override { cout << "SportsCar starts quickly" << endl; }
};

int main() {
    Vehicle* v = new SportsCar();
    v->start();
    delete v;
    return 0;
}
AVehicle starts
BCar starts
CSportsCar starts quickly
DCompilation error due to override
Attempts:
2 left
πŸ’‘ Hint
Virtual functions allow the most derived class's method to run through base class pointer.
❓ Predict Output
advanced
2:00remaining
Output of Hierarchical Inheritance with Method Calls
What is the output of this C++ code demonstrating hierarchical inheritance?
C++
#include <iostream>
using namespace std;

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

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

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

int main() {
    Circle c;
    Square s;
    c.draw();
    s.draw();
    return 0;
}
ADrawing circle\nDrawing square
BDrawing shape\nDrawing shape
CDrawing circle\nDrawing shape
DCompilation error due to draw() redefinition
Attempts:
2 left
πŸ’‘ Hint
Each derived class has its own draw() method that hides the base class method.
🧠 Conceptual
expert
3:00remaining
Diamond Problem in Multiple Inheritance
In C++, what problem arises in multiple inheritance when two base classes inherit from the same grandparent class, and a derived class inherits from both base classes? What is the typical solution?
AAmbiguity in accessing grandparent members; solved by virtual inheritance
BCompilation error due to duplicate class names; solved by renaming classes
CRuntime error due to conflicting constructors; solved by deleting one base class
DNo problem arises; multiple inheritance always works without issues
Attempts:
2 left
πŸ’‘ Hint
Think about how many copies of the grandparent class exist in the derived class.

Practice

(1/5)
1. Which type of inheritance involves a single derived class inheriting from one base class in C++?
easy
A. Hierarchical inheritance
B. Multiple inheritance
C. Multilevel inheritance
D. Single inheritance

Solution

  1. Step 1: Understand inheritance types

    Single inheritance means one class inherits from exactly one base class.
  2. Step 2: Match definition to options

    Only Single inheritance describes a single derived class inheriting from one base class.
  3. Final Answer:

    Single inheritance -> Option D
  4. Quick Check:

    One derived, one base = Single inheritance [OK]
Hint: One derived from one base is single inheritance [OK]
Common Mistakes:
  • Confusing multiple inheritance with single inheritance
  • Thinking multilevel means one base class only
  • Mixing hierarchical with single inheritance
2. Which of the following is the correct syntax for multiple inheritance in C++?
easy
A. class Derived : public Base1 & Base2 {};
B. class Derived : public Base1, public Base2 {};
C. class Derived : Base1 Base2 {};
D. class Derived inherits Base1, Base2 {};

Solution

  1. Step 1: Recall multiple inheritance syntax

    In C++, multiple inheritance uses commas to separate base classes with access specifiers.
  2. Step 2: Check each option

    class Derived : public Base1, public Base2 {}; uses correct syntax: 'class Derived : public Base1, public Base2 {};'. Others have syntax errors.
  3. Final Answer:

    class Derived : public Base1, public Base2 {}; -> Option B
  4. Quick Check:

    Comma separates base classes with access specifiers [OK]
Hint: Use commas and access specifiers for multiple inheritance [OK]
Common Mistakes:
  • Omitting commas between base classes
  • Using '&' instead of commas
  • Writing 'inherits' keyword which doesn't exist in C++
3. What will be the output of the following C++ code?
class A { public: void show() { std::cout << "A"; } };
class B : public A { public: void show() { std::cout << "B"; } };
class C : public B {};
int main() {
  C obj;
  obj.show();
  return 0;
}
medium
A. B
B. A
C. C
D. Compilation error

Solution

  1. Step 1: Understand inheritance chain

    Class C inherits from B, which inherits from A. B overrides show() method.
  2. Step 2: Determine which show() is called

    Calling obj.show() on C calls B's show() because C inherits B's version and does not override it.
  3. Final Answer:

    B -> Option A
  4. Quick Check:

    Derived class method called if overridden [OK]
Hint: Derived class method overrides base method unless not overridden [OK]
Common Mistakes:
  • Assuming base class method runs instead of overridden
  • Expecting 'C' output though C has no show()
  • Thinking code causes compilation error
4. Identify the error in this C++ code snippet involving inheritance:
class Base { public: int x; };
class Derived : Base {};
int main() {
  Derived d;
  d.x = 1;
  return 0;
}
medium
A. Main function missing return type
B. Base class must be declared inside Derived
C. Missing access specifier before Base in Derived class
D. Derived class cannot be empty

Solution

  1. Step 1: Check inheritance syntax

    In C++, inheritance requires an access specifier like public, private, or protected before base class name.
  2. Step 2: Identify the error

    Derived : Base {} lacks access specifier; should be Derived : public Base {}.
  3. Final Answer:

    Missing access specifier before Base in Derived class -> Option C
  4. Quick Check:

    Inheritance needs access specifier [OK]
Hint: Always specify public/private/protected before base class [OK]
Common Mistakes:
  • Omitting access specifier in inheritance
  • Thinking empty class body is error
  • Confusing return type in main function
5. Given the following classes:
class Animal { public: void eat() { std::cout << "Eating"; } };
class Mammal : public Animal {};
class Bird : public Animal {};
class Bat : public Mammal, public Bird {};
int main() {
  Bat b;
  b.eat();
  return 0;
}

What problem arises and how to fix it?
hard
A. Ambiguity error due to multiple inheritance; fix with virtual inheritance
B. No problem; code runs fine
C. Syntax error in Bat class declaration
D. eat() method must be overridden in Bat

Solution

  1. Step 1: Analyze inheritance structure

    Bat inherits Mammal and Bird, both inherit Animal, causing two Animal copies in Bat.
  2. Step 2: Identify ambiguity

    Calling b.eat() is ambiguous because Bat has two Animal parts; compiler error occurs.
  3. Step 3: Fix with virtual inheritance

    Use virtual inheritance for Mammal and Bird from Animal to share one Animal base.
  4. Final Answer:

    Ambiguity error due to multiple inheritance; fix with virtual inheritance -> Option A
  5. Quick Check:

    Diamond problem fixed by virtual inheritance [OK]
Hint: Use virtual inheritance to solve diamond ambiguity [OK]
Common Mistakes:
  • Ignoring diamond problem ambiguity
  • Thinking code compiles without error
  • Assuming method override fixes ambiguity
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