Skip to content
QuiznExam

Inheritance in C++

Test your knowledge of inheritance in C++, including base and derived classes, types of inheritance, access specifiers, constructor and destructor behavior, overriding, and multiple inheritance.

15
Questions
12 min
Time limit
Medium
Difficulty

No sign-up needed · Last updated:

Save your score

At least 8 characters.

Forgot password?

Related quizzes

All 15 questions and answers Spoilers: this shows the questions and answers. Try the quiz first! Open
  1. 1. What is inheritance in C++?

    • A. A mechanism that allows a class to acquire properties and behavior from another class
    • B. A mechanism used only to allocate memory
    • C. A method for creating namespaces
    • D. A feature used only for exception handling
    Show answer

    A. A mechanism that allows a class to acquire properties and behavior from another class

    Inheritance allows a derived class to reuse and extend members of a base class.

  2. 2. Which class inherits from another class in C++?

    • A. Derived class
    • B. Base class
    • C. Parent namespace
    • D. Friend class
    Show answer

    A. Derived class

    A derived class inherits members and behavior from a base class.

  3. 3. Which symbol is used to specify inheritance in a C++ class declaration?

    • A. :
    • B. ->
    • C. ::
    • D. #
    Show answer

    A. :

    A colon is placed after the derived class name to specify its base class and inheritance access, such as class Dog : public Animal.

  4. 4. Which of the following is a valid example of public inheritance?

    • A. class Dog : public Animal { };
    • B. class Dog -> public Animal { };
    • C. class Dog inherits Animal { };
    • D. class Dog extends Animal { };
    Show answer

    A. class Dog : public Animal { };

    C++ specifies inheritance using a colon followed by an access specifier and the base class name.

  5. 5. Which type of inheritance represents a relationship where one derived class inherits from one base class?

    • A. Single inheritance
    • B. Multiple inheritance
    • C. Hybrid inheritance
    • D. Hierarchical inheritance
    Show answer

    A. Single inheritance

    Single inheritance occurs when one derived class inherits from one base class.

  6. 6. What is multiple inheritance in C++?

    • A. A derived class inherits from more than one base class
    • B. Multiple derived classes inherit from one base class
    • C. One class contains multiple constructors
    • D. A class has multiple objects
    Show answer

    A. A derived class inherits from more than one base class

    Multiple inheritance allows a single derived class to inherit from two or more base classes.

  7. 7. Which access level allows a derived class to access inherited members while restricting direct access from unrelated outside code?

    • A. protected
    • B. private
    • C. public
    • D. static
    Show answer

    A. protected

    Protected members are accessible within the class itself and its derived classes, subject to C++ access rules.

  8. 8. What happens to public base-class members under public inheritance?

    • A. They remain public in the derived class
    • B. They become private
    • C. They become protected
    • D. They are automatically deleted
    Show answer

    A. They remain public in the derived class

    With public inheritance, public base-class members remain public in the derived class.

  9. 9. What happens to public and protected base-class members under private inheritance?

    • A. They become private members of the derived class
    • B. They remain public
    • C. They become protected only
    • D. They are inaccessible even inside the derived class
    Show answer

    A. They become private members of the derived class

    Under private inheritance, public and protected members inherited from the base class become private members of the derived class.

  10. 10. In what order are constructors called when creating an object of a derived class?

    • A. Base class constructor, then derived class constructor
    • B. Derived class constructor, then base class constructor
    • C. Only the derived class constructor
    • D. Constructors are called in random order
    Show answer

    A. Base class constructor, then derived class constructor

    When a derived object is created, its base-class subobjects are initialized before the derived class constructor body runs.

  11. 11. In what order are destructors called when a derived object is destroyed?

    • A. Derived class destructor, then base class destructor
    • B. Base class destructor, then derived class destructor
    • C. Only the base class destructor
    • D. Destructors are never called automatically
    Show answer

    A. Derived class destructor, then base class destructor

    Destruction occurs in reverse order of construction, so the derived destructor runs before the base class destructor.

  12. 12. What is method overriding in C++?

    • A. Providing a derived-class implementation of a virtual function declared in a base class
    • B. Defining two functions with different names
    • C. Creating multiple constructors in one class
    • D. Changing a variable from private to public
    Show answer

    A. Providing a derived-class implementation of a virtual function declared in a base class

    A derived class can override a virtual function from its base class to provide specialized behavior.

  13. 13. Which keyword is used to declare a virtual function in a C++ base class?

    • A. virtual
    • B. override
    • C. inherit
    • D. dynamic
    Show answer

    A. virtual

    The virtual keyword enables dynamic dispatch for a member function when accessed through a suitable base-class interface.

  14. 14. Which keyword can be used in a derived class to explicitly indicate that a function overrides a virtual base-class function?

    • A. override
    • B. virtualize
    • C. extends
    • D. inherits
    Show answer

    A. override

    The override specifier tells the compiler that the derived-class function is intended to override a virtual function from a base class.

  15. 15. What is hierarchical inheritance in C++?

    • A. Multiple derived classes inherit from the same base class
    • B. One derived class inherits from multiple base classes
    • C. A class inherits from itself
    • D. A class contains multiple constructors
    Show answer

    A. Multiple derived classes inherit from the same base class

    Hierarchical inheritance occurs when two or more derived classes inherit from a common base class.