AlgoMaster Logo

Inheritance - Quiz

Last Updated: January 3, 2026

1 min read

Inheritance Quiz

30 quizzes

1
Multiple Choice

In public inheritance, how are public and protected members of the base class treated in the derived class interface?

2
Multiple Choice

Which scenario is the most appropriate use of single inheritance?

3
Multiple Choice

Given class Base and class Derived : private Base, how can client code access Base's public members through Derived?

4
Multiple Choice

In C++, when constructing a derived object, in which order are constructors called?

5
Multiple Choice

What is the main reason to use virtual inheritance?

6
Multiple Choice

In multiple inheritance, how do you resolve ambiguity when two base classes define a method with the same name?

7
Multiple Choice

Which access specifier for inheritance best models 'is-a and can be used wherever base is expected'?

8
Multiple Choice

Which statement about constructors in inheritance is correct?

9
Multiple Choice

In a diamond hierarchy without virtual inheritance, class D : public B, public C, both derived from A, what does a D object contain?

10
Multiple Choice

When should you prefer composition over inheritance in C++?

11
Code Completion

Call a base class constructor with a parameter from a derived class constructor

cpp
1
class Derived : public Base {
2
public:
3
Derived(int x) : (x) {}
4
};

Click an option to fill the blank:

12
Code Completion

Declare a derived class using protected inheritance

cpp
1
class Derived : Base { };

Click an option to fill the blank:

13
Code Completion

Use virtual inheritance to avoid duplicate A subobjects

cpp
1
class B : public A {};

Click an option to fill the blank:

14
Output Prediction

What is the output?

1#include <iostream>
2
3class Animal {
4public:
5    void speak() const { std::cout << "Animal"; }
6};
7
8class Dog : public Animal {
9public:
10    void speak() const { std::cout << "Dog"; }
11};
12
13int main() {
14    Dog d;
15    Animal &ref = d;
16    ref.speak();
17}
18
15
Output Prediction

What is the output regarding constructor calls?

1#include <iostream>
2
3class Base {
4public:
5    Base() { std::cout << "B"; }
6};
7
8class Derived : public Base {
9public:
10    Derived() { std::cout << "D"; }
11};
12
13int main() {
14    Derived d;
15}
16
16
Output Prediction

What does this program print?

1#include <iostream>
2
3class Base {
4public:
5    virtual void f() { std::cout << "B"; }
6    virtual ~Base() = default;
7};
8
9class Derived : public Base {
10public:
11    void f() override { std::cout << "D"; }
12};
13
14void call(Base b) {
15    b.f();
16}
17
18int main() {
19    Derived d;
20    call(d);
21}
22
17
Output Prediction

What is the output with multiple inheritance?

1#include <iostream>
2
3class Logger {
4public:
5    void log() const { std::cout << "L"; }
6};
7
8class Notifier {
9public:
10    void notify() const { std::cout << "N"; }
11};
12
13class Service : public Logger, public Notifier {
14};
15
16int main() {
17    Service s;
18    s.log();
19    s.notify();
20}
21
18
Output Prediction

What is the output demonstrating virtual inheritance construction order?

1#include <iostream>
2
3class A {
4public:
5    A() { std::cout << "A"; }
6};
7
8class B : virtual public A {
9public:
10    B() { std::cout << "B"; }
11};
12
13class C : virtual public A {
14public:
15    C() { std::cout << "C"; }
16};
17
18class D : public B, public C {
19public:
20    D() { std::cout << "D"; }
21};
22
23int main() {
24    D d;
25}
26
19
Bug Spotting

Find the bug related to object lifetime in this inheritance example

Click on the line(s) that contain the bug.

cpp
1
#include <iostream>
2
 
3
class Base {
4
public:
5
    Base() { std::cout << "Base"; }
6
    ~Base() { std::cout << "~Base"; }
7
};
8
 
9
class Derived : public Base {
10
public:
11
    ~Derived() { std::cout << "~Derived"; }
12
};
13
 
14
void destroy(Base* p) {
15
    delete p;
16
}
17
 
18
int main() {
19
    Derived* d = new Derived();
20
    destroy(d);
21
}
22
 
20
Bug Spotting

Find the bug related to constructor initialization in multiple inheritance

Click on the line(s) that contain the bug.

cpp
1
#include <string>
2
 
3
class Named {
4
public:
5
    Named(const std::string& n) : name(n) {}
6
private:
7
    std::string name;
8
};
9
 
10
class Sized {
11
public:
12
    Sized(int s) : size(s) {}
13
private:
14
    int size;
15
};
16
 
17
class Widget : public Named, public Sized {
18
public:
19
    Widget() {
20
        // attempt to default-construct bases
21
    }
22
};
23
 
21
Hotspot Selection

Click the line where ambiguity arises due to multiple inheritance

Click on the line to select.

cpp
1
#include <iostream>
2
 
3
class A {
4
public:
5
    void print() const { std::cout << "A"; }
6
};
7
 
8
class B : public A {};
9
class C : public A {};
10
 
11
class D : public B, public C {};
12
 
13
int main() {
14
    D d;
15
    d.print();
16
}
17
 
22
Hotspot Selection

Click the line that uses virtual inheritance to solve the diamond problem

Click on the line to select.

cpp
1
class A {
2
public:
3
    int value;
4
};
5
 
6
class B : virtual public A {};
7
class C : virtual public A {};
8
 
9
class D : public B, public C {
10
public:
11
    void set(int v) { value = v; }
12
};
13
 
23
Fill in the Blanks

Complete the class definition to use public inheritance and override a virtual function

cpp
1
#include <iostream>
2
3
class Base {
4
public:
5
virtual void run() const { std::cout << "Base"; }
6
};
7
8
class Derived : Base {
9
public:
10
void run() const { std::cout << "Derived"; }
11
};
12

Click an option to fill blank 1:

24
Fill in the Blanks

Use a constructor initializer list to call the base class constructor

cpp
1
#include <string>
2
3
class User {
4
public:
5
User(const std::string& name) : name_(name) {}
6
private:
7
std::string name_;
8
};
9
10
class Admin : public User {
11
public:
12
Admin(const std::string& name, int level)
13
: (name), level_() {}
14
private:
15
int level_;
16
};
17

Click an option to fill blank 1:

25
Fill in the Blanks

Complete the declarations to model virtual inheritance in a diamond hierarchy

cpp
1
class Base {
2
public:
3
int id;
4
};
5
6
class Left : public Base {};
7
class Right : public Base {};
8
9
class Final : public Left, public Right {
10
public:
11
void set(int v) { id = v; }
12
};
13

Click an option to fill blank 1:

26
Fill in the Blanks

Control access to base members via private inheritance and a public wrapper

cpp
1
class Logger {
2
public:
3
void log(const char* msg) const;
4
};
5
6
class Service : Logger {
7
public:
8
void info(const char* msg) const {
9
(msg);
10
}
11
};
12

Click an option to fill blank 1:

27
Matching

Match the inheritance-related terms with their descriptions

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

28
Matching

Match the access specifiers in inheritance with resulting visibility of base public members in the derived class

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

29
Sequencing

Order the steps when constructing an object with virtual inheritance: class D : public B, public C; class B : virtual public A; class C : virtual public A;

Drag and drop to reorder, or use the arrows.

30
Sequencing

Order these actions when designing an inheritance hierarchy for a new feature

Drag and drop to reorder, or use the arrows.