C++ Fundamentals: OOP Inheritance, Polymorphism, and Encapsulation Quiz

Explore key concepts of C++ object-oriented programming with this quiz focused on inheritance, polymorphism, and encapsulation. Strengthen your understanding of classes, access specifiers, virtual functions, and related terminology essential for C++ programming.

  1. Understanding Inheritance

    In C++, which keyword is used to derive a new class from an existing class, enabling inheritance?

    1. class
    2. inherits
    3. extends
    4. public

    Explanation: The 'public' keyword is used in the class declaration to specify public inheritance in C++. 'extends' and 'inherits' are common in other languages but not in C++. The 'class' keyword is used to define a class but does not specify inheritance. Only 'public' correctly applies for inheritance access specification.

  2. Access Specifiers and Data Hiding

    Which access specifier in C++ allows class members to be accessed only within the same class and not by derived classes?

    1. public
    2. package
    3. protected
    4. private

    Explanation: The 'private' access specifier restricts access to class members so they can be used only within the class itself. 'protected' allows access in derived classes, while 'public' permits access from anywhere. 'package' is not valid in C++. Therefore, 'private' is the correct choice for strict data hiding.

  3. Polymorphism in Practice

    Which C++ feature enables the same function name to be used for different types or numbers of parameters in the same scope?

    1. Function overloading
    2. Reusability
    3. Inheritance
    4. Encapsulation

    Explanation: Function overloading permits multiple functions with the same name to exist as long as their parameter lists are different. Inheritance relates to class relationships and reusability, while encapsulation is about bundling data and functions. 'Function overloading' is the correct term for using the same function name with different parameter profiles.

  4. Virtual Functions and Binding

    In C++, what kind of function allows for dynamic or late binding, enabling run-time polymorphism?

    1. Virtual function
    2. Static function
    3. Constructor
    4. Template function

    Explanation: A 'virtual function' enables run-time polymorphism in C++, allowing the correct function to be called according to the actual object type rather than the pointer type. 'Static functions' and 'constructors' do not support polymorphism. 'Template functions' are related to generic programming, not run-time binding.

  5. Encapsulation Uses

    What does encapsulation primarily achieve in C++ object-oriented programming?

    1. Overriding
    2. Code duplication
    3. Multiple inheritance
    4. Data hiding

    Explanation: Encapsulation is the concept of bundling data and methods that operate on that data into a single unit, typically a class, and restricting access to some components. This leads to data hiding. Overriding is related to polymorphism; code duplication is not encouraged, and multiple inheritance is a different concept.

  6. Base vs Derived Behavior

    Given class Base and class Derived : public Base, what is true about the relationship between these two classes?

    1. Derived inherits from Base
    2. Derived is a superclass of Base
    3. Derived hides Base
    4. Base inherits from Derived

    Explanation: In 'class Derived : public Base', 'Derived' is the subclass and inherits properties and behaviors from 'Base'. 'Derived is a superclass' is incorrect; 'Base inherits from Derived' reverses the relationship. 'Derived hides Base' is not the standard terminology for this situation.

  7. Protected Members

    Which access specifier allows derived classes to access members of the base class, but not external code?

    1. protected
    2. internal
    3. public
    4. private

    Explanation: 'protected' members are accessible in derived classes but hidden from outside the class hierarchy. 'private' restricts access to the class itself only, while 'public' permits access from everywhere. 'internal' is not a valid access specifier in C++.

  8. Pure Virtual Functions

    What is the correct syntax for declaring a pure virtual function in a C++ abstract base class?

    1. void func() = 1;
    2. virtual func() {};
    3. virtual void func() = 0;
    4. func() = default;

    Explanation: A pure virtual function is declared using the 'virtual' keyword followed by '= 0;'. 'void func() = 1;' is incorrect syntax. 'virtual func() {};' is not pure virtual, and 'func() = default;' is used for default implementations, not abstraction.

  9. Object Slicing

    What term describes the loss of information when a derived class object is assigned to a base class object in C++?

    1. Shadowing
    2. Object slicing
    3. Type erasure
    4. Function hiding

    Explanation: Object slicing occurs when parts of a derived object not present in the base class are lost upon assignment. 'Shadowing' and 'function hiding' involve names being hidden, and 'type erasure' refers to generic programming concepts not directly related here.

  10. Accessing Base Class Functions

    If a derived class overrides a method from its base class, which syntax can be used to call the base class’s version of the method from the derived class in C++?

    1. method.base();
    2. super.method();
    3. base.method();
    4. Base::method();

    Explanation: To access the base class method in C++, you use 'Base::method();'. 'base.method();' and 'method.base();' are not valid C++ syntax. 'super.method();' is used in some other languages but not in standard C++.