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.
In C++, which keyword is used to derive a new class from an existing class, enabling inheritance?
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.
Which access specifier in C++ allows class members to be accessed only within the same class and not by derived classes?
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.
Which C++ feature enables the same function name to be used for different types or numbers of parameters in the same scope?
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.
In C++, what kind of function allows for dynamic or late binding, enabling run-time polymorphism?
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.
What does encapsulation primarily achieve in C++ object-oriented programming?
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.
Given class Base and class Derived : public Base, what is true about the relationship between these two classes?
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.
Which access specifier allows derived classes to access members of the base class, but not external code?
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++.
What is the correct syntax for declaring a pure virtual function in a C++ abstract base class?
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.
What term describes the loss of information when a derived class object is assigned to a base class object in C++?
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.
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++?
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++.