OOP Quiz - Languages C++
Free practice quiz: OOP (Languages C++). 16 multiple-choice questions with answers and explanations. Play online, no sign-up needed.
Play all 16 questions freeSample questions
Which of the following is NOT a fundamental principle of Object-Oriented Programming (OOP)?
- Encapsulation
- Inheritance
- Polymorphism
- Procedural Abstraction
Show answer
Answer: D. Procedural Abstraction
Encapsulation, Inheritance, and Polymorphism are core principles of OOP. Procedural Abstraction is a concept related to procedural programming, not OOP directly.
What is the primary purpose of inheritance in C++ OOP?
- To restrict access to member variables.
- To create new classes that reuse, extend, and modify the behavior defined in other classes.
- To define constant values within a class.
- To ensure that a class can only have one instance.
Show answer
Answer: B. To create new classes that reuse, extend, and modify the behavior defined in other classes.
Inheritance allows a new class (derived class) to inherit properties and behaviors from an existing class (base class), promoting code reusability and establishing an 'is-a' relationship.
What is a pure virtual function in C++?
- A virtual function that has no body.
- A virtual function that can only be called from the base class.
- A virtual function that is automatically overridden by derived classes.
- A virtual function that returns a void type.
Show answer
Answer: A. A virtual function that has no body.
A pure virtual function is declared by appending `= 0` to its declaration. It compels derived classes to provide an implementation for this function, making the base class abstract.
Consider a class `A` with a protected member `y`. If class `B` inherits privately from `A`, what is the access specifier of `y` in class `B`?
- Public
- Protected
- Private
- Inaccessible
Show answer
Answer: C. Private
In private inheritance, protected members of the base class become private members of the derived class. Public members of the base class also become private members of the derived class.
What is the purpose of the `this` pointer in C++?
- To point to the next object in a collection.
- To refer to the current object within a member function.
- To point to the base class.
- To point to the global scope.
Show answer
Answer: B. To refer to the current object within a member function.
The `this` pointer is a keyword available in C++ member functions. It holds the memory address of the object for which the member function is currently being invoked.