C++ OOP: Inheritance Quiz

C++
0 Passed
0% acceptance

40 in-depth questions covering C++ inheritance fundamentals including base and derived classes, protected members, method overriding, final and override keywords, and inheritance best practices — with 16 code examples to master object-oriented inheritance patterns.

40 Questions
~80 minutes
1

Question 1

What is inheritance in C++?

A
A mechanism where a derived class inherits properties and behaviors from a base class, enabling code reuse and establishing 'is-a' relationships between classes
B
A way to create multiple copies of a class
C
A method for combining two classes into one
D
A technique for hiding class implementation details
2

Question 2

What is the syntax for inheritance in C++?

cpp
class Base {
public:
    void method() {}
};

class Derived : public Base {
    // Inherits method() from Base
};

int main() {
    Derived d;
    d.method();  // Can call inherited method
    return 0;
}
A
class Derived : access-specifier Base { ... }; where access-specifier is public, protected, or private, determining how base class members are accessible in derived class
B
class Derived extends Base { ... };
C
class Derived(Base) { ... };
D
inherit class Derived from Base { ... };
3

Question 3

What is the difference between public, protected, and private inheritance?

cpp
class Base {
public:
    int pub;
protected:
    int prot;
private:
    int priv;
};

class PubDerived : public Base {
    // pub is public, prot is protected, priv is inaccessible
};

class ProtDerived : protected Base {
    // pub is protected, prot is protected, priv is inaccessible
};

class PrivDerived : private Base {
    // pub is private, prot is private, priv is inaccessible
};
A
Public inheritance preserves access levels, protected inheritance makes all inherited members protected, private inheritance makes all inherited members private
B
They are identical in functionality
C
Private inheritance is the most common type
D
Protected inheritance hides all base class members
4

Question 4

What are protected members in inheritance?

cpp
class Base {
protected:
    int protectedData;
    void protectedMethod() {}
public:
    void publicMethod() {
        protectedData = 42;  // OK
        protectedMethod();    // OK
    }
};

class Derived : public Base {
public:
    void derivedMethod() {
        protectedData = 24;   // OK - accessible in derived class
        protectedMethod();     // OK - accessible in derived class
    }
};

int main() {
    Derived d;
    // d.protectedData = 10;  // Error - not accessible outside hierarchy
    d.derivedMethod();       // OK
    return 0;
}
A
Members accessible within the class and its derived classes but not from outside the inheritance hierarchy, providing encapsulation while allowing derived class access
B
Members that are completely hidden from derived classes
C
Members that are public to everyone
D
Members that can only be accessed by the base class
5

Question 5

What is method overriding in inheritance?

cpp
class Base {
public:
    virtual void method() {
        std::cout << "Base method" << std::endl;
    }
};

class Derived : public Base {
public:
    void method() override {  // Override keyword is optional but recommended
        std::cout << "Derived method" << std::endl;
    }
};

int main() {
    Base* b = new Derived();
    b->method();  // Calls Derived::method() due to virtual dispatch
    delete b;
    return 0;
}
A
When a derived class provides a new implementation for a virtual method from the base class, enabling polymorphic behavior through dynamic dispatch
B
When a derived class hides a base class method with the same name
C
When a derived class calls a base class method
D
When inheritance is prevented
6

Question 6

What is the difference between overriding and hiding in inheritance?

cpp
class Base {
public:
    void method(int x) {
        std::cout << "Base: " << x << std::endl;
    }
    virtual void virtMethod() {
        std::cout << "Base virtual" << std::endl;
    }
};

class Derived : public Base {
public:
    void method() {  // Hides Base::method(int)
        std::cout << "Derived" << std::endl;
    }
    void virtMethod() override {  // Overrides Base::virtMethod()
        std::cout << "Derived virtual" << std::endl;
    }
};

int main() {
    Derived d;
    d.method();      // Calls Derived::method()
    // d.method(5);   // Error - Base::method(int) is hidden
    d.virtMethod();  // Calls Derived::virtMethod()
    return 0;
}
A
Overriding replaces virtual base methods for polymorphism, hiding makes base methods inaccessible when derived class defines same name, requiring explicit qualification to access hidden methods
B
They are identical concepts
C
Hiding only occurs with virtual methods
D
Overriding requires the override keyword
7

Question 7

What is the override keyword in C++?

cpp
class Base {
public:
    virtual void method() {}
    virtual void method(int x) {}
};

class Derived : public Base {
public:
    void method() override {  // Correct override
        // Implementation
    }
    
    // void method(int x) override {  // Error: no matching base method
    // }
    
    void method(float f) override {  // Error: signature mismatch
    }
};
A
A keyword that ensures a method actually overrides a virtual base class method, preventing bugs from signature mismatches or missing virtual declarations
B
A keyword that makes methods virtual
C
A keyword that prevents method overriding
D
A keyword that hides base class methods
8

Question 8

What is the final keyword in inheritance?

cpp
class Base final {  // Cannot be inherited from
public:
    virtual void method() {}
};

// class Derived : public Base {};  // Error: cannot inherit from final class

class NonFinal {
public:
    virtual void method() {}
    virtual void finalMethod() final {}  // Cannot be overridden
};

class Derived : public NonFinal {
public:
    void method() override {}  // OK
    // void finalMethod() override {}  // Error: cannot override final method
};
A
A keyword that prevents inheritance (when applied to classes) or overriding (when applied to virtual methods), ensuring class or method implementations cannot be changed
B
A keyword that makes classes inheritable
C
A keyword that forces method overriding
D
A keyword that hides inherited methods
9

Question 9

What is the order of constructor and destructor calls in inheritance?

cpp
class Base {
public:
    Base() { std::cout << "Base constructor" << std::endl; }
    ~Base() { std::cout << "Base destructor" << std::endl; }
};

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

int main() {
    Derived d;  // Output: Base constructor, Derived constructor
    return 0;   // Output: Derived destructor, Base destructor
}
A
Base class constructors run before derived class constructors, base class destructors run after derived class destructors, ensuring proper initialization and cleanup order
B
Derived constructors run before base constructors
C
All constructors run simultaneously
D
Destructor order is the same as constructor order
10

Question 10

What is the base class initializer syntax in derived constructors?

cpp
class Base {
protected:
    int value;
public:
    Base(int v) : value(v) {}
};

class Derived : public Base {
private:
    std::string name;
public:
    Derived(int v, const std::string& n) 
        : Base(v), name(n) {  // Initialize base class first
    }
};

int main() {
    Derived d(42, "test");
    return 0;
}
A
Derived constructor uses member initializer list with Base(parameters) to explicitly initialize the base class before derived class members
B
Base class is initialized automatically with default constructor
C
Derived constructor calls base constructor in the body
D
Base class initialization is not allowed in derived constructors
11

Question 11

What is a virtual destructor and why is it important?

cpp
class Base {
public:
    Base() { std::cout << "Base created" << std::endl; }
    virtual ~Base() { std::cout << "Base destroyed" << std::endl; }  // Virtual!
    virtual void method() = 0;
};

class Derived : public Base {
public:
    Derived() { std::cout << "Derived created" << std::endl; }
    ~Derived() { std::cout << "Derived destroyed" << std::endl; }
    void method() override {}
};

int main() {
    Base* b = new Derived();  // Polymorphic creation
    delete b;  // Correctly calls ~Derived() then ~Base()
    return 0;
}
A
A virtual destructor ensures proper cleanup when deleting derived objects through base class pointers, preventing memory leaks and undefined behavior
B
A destructor that can be overridden in derived classes
C
A destructor that prevents inheritance
D
A destructor that is called automatically
12

Question 12

What is the difference between composition and inheritance?

cpp
// Inheritance (is-a relationship)
class Engine {
public:
    void start() {}
};

class Car : public Engine {  // Car is-an Engine? No!
};

// Composition (has-a relationship) - Better design
class Engine {
public:
    void start() {}
};

class Car {
private:
    Engine engine;  // Car has-an Engine
public:
    void start() { engine.start(); }
};
A
Inheritance creates 'is-a' relationships with tight coupling, composition creates 'has-a' relationships with loose coupling and better encapsulation
B
They are identical design patterns
C
Composition is always better than inheritance
D
Inheritance provides better encapsulation than composition
13

Question 13

What is the diamond problem in multiple inheritance?

cpp
class A {
public:
    void method() { std::cout << "A" << std::endl; }
};

class B : public A {};
class C : public A {};

class D : public B, public C {  // Diamond inheritance
    // D inherits from two copies of A!
    // Which method() should D use?
};

int main() {
    D d;
    // d.method();  // Ambiguous - which A::method()?
    return 0;
}
A
Ambiguity when a class inherits from multiple classes that share a common base class, causing duplicate base class instances and method call ambiguities
B
A problem with single inheritance only
C
A problem that doesn't exist in C++
D
A problem with virtual inheritance
14

Question 14

What is virtual inheritance and how does it solve the diamond problem?

cpp
class A {
public:
    void method() { std::cout << "A" << std::endl; }
};

class B : virtual public A {};  // Virtual inheritance
class C : virtual public A {};  // Virtual inheritance

class D : public B, public C {  // Diamond with virtual inheritance
public:
    void callMethod() { method(); }  // No ambiguity - only one A
};

int main() {
    D d;
    d.callMethod();  // OK - calls A::method()
    return 0;
}
A
Virtual inheritance ensures only one instance of the common base class exists in diamond hierarchies, eliminating ambiguity through shared base class subobjects
B
Virtual inheritance prevents multiple inheritance
C
Virtual inheritance makes all methods virtual
D
Virtual inheritance is the same as regular inheritance
15

Question 15

What is the difference between using-declaration and inheritance access specifiers?

cpp
class Base {
private:
    void privateMethod() {}
protected:
    void protectedMethod() {}
public:
    void publicMethod() {}
};

class Derived : private Base {  // Private inheritance
public:
    using Base::protectedMethod;  // Make protectedMethod public
    using Base::publicMethod;     // Make publicMethod public
    // Cannot use privateMethod - still private
};

int main() {
    Derived d;
    d.publicMethod();     // OK - made public via using
    d.protectedMethod();  // OK - made public via using
    return 0;
}
A
Inheritance access specifiers change all inherited members' visibility, using-declarations selectively change individual member visibility within the inheritance hierarchy
B
They are identical in functionality
C
Using-declarations can only make members more private
D
Inheritance access specifiers are more powerful than using-declarations
16

Question 16

What is the Liskov Substitution Principle in inheritance?

A
Derived classes must be substitutable for their base classes without altering program correctness, ensuring derived objects behave consistently with base class contracts
B
Derived classes must have the same method names as base classes
C
Derived classes must inherit all private members
D
Derived classes must be smaller than base classes
17

Question 17

What is the fragile base class problem?

A
Changes to base class implementation can break derived classes unexpectedly, especially when adding new methods or changing virtual function behavior
B
Base classes that cannot be inherited from
C
Base classes with too many virtual methods
D
Base classes that are too large
18

Question 18

What is the difference between interface inheritance and implementation inheritance?

cpp
// Interface inheritance (pure virtual)
class IShape {
public:
    virtual double area() const = 0;  // Interface only
    virtual ~IShape() = default;
};

// Implementation inheritance
class Shape {
public:
    virtual double area() const { return 0.0; }  // Default implementation
    void draw() const { /* common drawing code */ }
};

class Circle : public IShape {  // Interface inheritance
public:
    double area() const override { return 3.14 * r * r; }
private:
    double r;
};

class ColoredShape : public Shape {  // Implementation inheritance
public:
    double area() const override { /* custom area */ }
};
A
Interface inheritance defines contracts through pure virtual methods, implementation inheritance provides default behaviors and code reuse through virtual methods with implementations
B
They are identical concepts
C
Interface inheritance is always better
D
Implementation inheritance doesn't use virtual methods
19

Question 19

What is the difference between covariant return types and regular overriding?

cpp
class Base {
public:
    virtual Base* clone() const { return new Base(*this); }
};

class Derived : public Base {
public:
    // Covariant return type - more specific than Base*
    Derived* clone() const override { return new Derived(*this); }
};

int main() {
    Derived d;
    Derived* d2 = d.clone();  // Can use Derived* directly
    Base* b = d.clone();      // Still works
    return 0;
}
A
Covariant return types allow overridden methods to return more specific types than the base method, enabling better type safety in inheritance hierarchies
B
They are identical concepts
C
Covariant returns are less type-safe
D
Regular overriding doesn't allow return type changes
20

Question 20

What is the difference between inheritance and templates for code reuse?

A
Inheritance provides runtime polymorphism with virtual dispatch overhead, templates provide compile-time polymorphism with no runtime overhead but code bloat
B
They are identical approaches
C
Inheritance is always faster than templates
D
Templates cannot be used for code reuse
21

Question 21

What is the difference between public inheritance and private inheritance?

A
Public inheritance creates 'is-a' relationships visible to clients, private inheritance creates implementation inheritance hidden from clients, often used for implementation details
B
They are identical in functionality
C
Private inheritance is more common than public inheritance
D
Public inheritance hides implementation details
22

Question 22

What is the difference between virtual and non-virtual inheritance?

A
Virtual inheritance shares base class subobjects to avoid duplication in diamond hierarchies, non-virtual inheritance creates separate base class instances
B
They are identical concepts
C
Virtual inheritance makes methods virtual
D
Non-virtual inheritance is only used for interfaces
23

Question 23

What is the difference between inheritance and delegation?

cpp
// Inheritance
class Stack : public std::vector<int> {  // Stack is-a vector
public:
    void push(int x) { push_back(x); }
    int pop() { int x = back(); pop_back(); return x; }
};

// Delegation (composition)
class Stack {
private:
    std::vector<int> data;  // Stack has-a vector
public:
    void push(int x) { data.push_back(x); }
    int pop() { int x = data.back(); data.pop_back(); return x; }
};
A
Inheritance exposes all base class methods to clients, delegation hides implementation details and exposes only intended interface, providing better encapsulation
B
They are identical design patterns
C
Delegation is always better than inheritance
D
Inheritance provides better performance than delegation
24

Question 24

What is the difference between method hiding and method overriding?

A
Overriding replaces virtual methods for polymorphism, hiding makes base methods inaccessible without qualification when derived methods have same name but different signatures
B
They are identical concepts
C
Hiding only occurs with virtual methods
D
Overriding requires different method signatures
25

Question 25

What is the difference between abstract classes and concrete classes in inheritance?

cpp
// Abstract class (cannot be instantiated)
class Shape {
public:
    virtual double area() const = 0;  // Pure virtual
    virtual ~Shape() = default;
    void draw() const { /* common implementation */ }
};

// Concrete class (can be instantiated)
class Circle : public Shape {
private:
    double radius;
public:
    Circle(double r) : radius(r) {}
    double area() const override { return 3.14159 * radius * radius; }
};

int main() {
    // Shape s;  // Error: abstract class
    Circle c(5.0);  // OK: concrete class
    return 0;
}
A
Abstract classes have pure virtual methods and cannot be instantiated, serving as interfaces or base classes with incomplete implementations, while concrete classes can be instantiated
B
They are identical concepts
C
Abstract classes are faster than concrete classes
D
Concrete classes cannot have virtual methods
26

Question 26

What is the difference between inheritance depth and inheritance breadth?

A
Depth refers to hierarchy height (base->derived->derived), breadth refers to number of direct subclasses, with deep hierarchies being fragile and wide hierarchies being complex
B
They are identical concepts
C
Depth is always better than breadth
D
Breadth refers to method count, depth refers to class count
27

Question 27

What is the difference between virtual method calls and non-virtual method calls in inheritance?

A
Virtual calls use dynamic dispatch for runtime polymorphism with overhead, non-virtual calls use static binding for direct calls with better performance
B
They are identical in performance
C
Virtual calls are always faster
D
Non-virtual calls cannot be overridden
28

Question 28

What is the difference between inheritance and polymorphism?

A
Inheritance enables code reuse through class hierarchies, polymorphism enables runtime method selection through virtual dispatch for flexible behavior
B
They are identical concepts
C
Polymorphism requires inheritance
D
Inheritance requires polymorphism
29

Question 29

What is the difference between base class pointers and derived class pointers in inheritance?

cpp
class Base {
public:
    virtual void method() { std::cout << "Base" << std::endl; }
};

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

int main() {
    Derived d;
    Base* bp = &d;     // Base pointer to derived object - OK
    // Derived* dp = bp;  // Error: cannot convert without cast
    Derived* dp = static_cast<Derived*>(bp);  // OK with cast
    
    bp->method();  // Calls Derived::method() (polymorphism)
    dp->method();  // Calls Derived::method()
    return 0;
}
A
Base pointers can point to derived objects enabling polymorphism, derived pointers cannot point to base objects without explicit casting and risk of invalid access
B
They are identical in functionality
C
Derived pointers are always safer than base pointers
D
Base pointers cannot access derived methods
30

Question 30

What is the difference between inheritance and subtyping?

A
Inheritance is implementation mechanism for code reuse, subtyping is behavioral contract ensuring derived objects can substitute for base objects (LSP)
B
They are identical concepts
C
Subtyping requires inheritance
D
Inheritance requires subtyping
31

Question 31

What is the difference between inheritance coupling and composition coupling?

A
Inheritance creates tight coupling where base class changes affect all derived classes, composition creates loose coupling where components can be replaced independently
B
They are identical in coupling strength
C
Composition is always more tightly coupled
D
Inheritance coupling is easier to manage
32

Question 32

What is the difference between virtual base classes and regular base classes?

A
Virtual base classes are shared in diamond hierarchies to avoid duplication, regular base classes create separate instances in multiple inheritance
B
They are identical concepts
C
Virtual base classes cannot have virtual methods
D
Regular base classes are only used for interfaces
33

Question 33

What is the difference between inheritance and mixins?

A
Inheritance creates hierarchical relationships, mixins provide composable functionality through multiple inheritance without primary base class coupling
B
They are identical concepts
C
Mixins require single inheritance
D
Inheritance provides better composition than mixins
34

Question 34

What is the difference between inheritance refactoring and composition refactoring?

A
Inheritance refactoring changes class hierarchies affecting all derived classes, composition refactoring changes object relationships with localized impact
B
They are identical processes
C
Composition refactoring is always easier
D
Inheritance refactoring doesn't affect derived classes
35

Question 35

What is the difference between inheritance documentation and composition documentation?

A
Inheritance documentation focuses on class hierarchies and behavioral contracts, composition documentation focuses on object interactions and dependencies
B
They are identical approaches
C
Composition documentation is more complex
D
Inheritance documentation doesn't include behavior
36

Question 36

What is the difference between inheritance testing and composition testing?

A
Inheritance testing verifies hierarchical behavior and LSP compliance, composition testing verifies object interactions and component substitutability
B
They are identical testing approaches
C
Composition testing is always easier
D
Inheritance testing doesn't verify behavior
37

Question 37

What is the difference between inheritance maintenance and composition maintenance?

A
Inheritance maintenance affects entire hierarchies when base classes change, composition maintenance is localized to component interactions
B
They are identical maintenance approaches
C
Composition maintenance is always harder
D
Inheritance maintenance doesn't affect derived classes
38

Question 38

What is the difference between inheritance performance and composition performance?

A
Inheritance may have virtual dispatch overhead, composition has indirection overhead but potentially better cache locality and optimization opportunities
B
They are identical in performance
C
Composition is always faster
D
Inheritance has no performance overhead
39

Question 39

What is the difference between inheritance evolution and composition evolution?

A
Inheritance evolution requires careful base class changes to avoid breaking derived classes, composition evolution allows independent component upgrades
B
They are identical evolution processes
C
Composition evolution is always harder
D
Inheritance evolution doesn't affect existing code
40

Question 40

What are the fundamental principles for effective inheritance design in C++?

A
Follow LSP for behavioral consistency, prefer composition over inheritance when possible, use final to prevent inappropriate inheritance, ensure virtual destructors for polymorphic deletion, and avoid deep inheritance hierarchies
B
Always use public inheritance for all class relationships
C
Make all methods virtual to enable maximum flexibility
D
Avoid composition and use inheritance for all code reuse

QUIZZES IN C++