CPP Object-Oriented Programming & Class Design 2 — Questions and Answers
Question 1: Which access specifier should be used for data members that must be accessible only within the class and its derived classes?
- public
- private
- protected (Correct answer)
- internal
Correct answer: protected
Protected members are accessible within the class itself and by derived classes, but not from outside the class hierarchy.
Question 2: What is the purpose of a copy constructor in C++?
- To create a default-initialized object
- To initialize an object as a copy of another object of the same type (Correct answer)
- To assign one object to another after both are constructed
- To destroy a copy of an object
Correct answer: To initialize an object as a copy of another object of the same type
A copy constructor initializes a new object by copying the state from an existing object of the same type.
Question 3: In C++, what does the 'explicit' keyword prevent when applied to a constructor?
- The constructor from being called with no arguments
- Implicit type conversions using that constructor (Correct answer)
- The constructor from being inherited
- The constructor from throwing exceptions
Correct answer: Implicit type conversions using that constructor
The explicit keyword prevents the compiler from using a constructor for implicit conversions or copy-initialization.
Question 4: What is object slicing in C++?
- Splitting a large class into smaller classes
- Losing derived-class data when a derived object is assigned to a base-class object by value (Correct answer)
- Dividing an array of objects into segments
- Cutting unused methods from a compiled class
Correct answer: Losing derived-class data when a derived object is assigned to a base-class object by value
Object slicing occurs when a derived class object is copied into a base class object by value, stripping away the derived portion.
Question 5: Which of the following correctly declares a pure virtual function in C++?
- virtual void draw() {}
- void draw() = 0;
- virtual void draw() = 0; (Correct answer)
- abstract void draw();
Correct answer: virtual void draw() = 0;
A pure virtual function is declared with 'virtual' and assigned '= 0', making the class abstract.
Question 6: What does the 'this' pointer represent inside a non-static member function?
- A pointer to the class definition
- A pointer to the calling object (Correct answer)
- A pointer to the base class subobject
- A pointer to the vtable
Correct answer: A pointer to the calling object
Inside a non-static member function, 'this' is an implicit pointer to the object on which the function was called.
Question 7: When a class declares a destructor as virtual, what is the primary benefit?
- The destructor can be called multiple times safely
- The correct derived-class destructor is called when deleting through a base-class pointer (Correct answer)
- The destructor runs before any member destructors
- The class becomes abstract
Correct answer: The correct derived-class destructor is called when deleting through a base-class pointer
A virtual destructor ensures that when a derived object is deleted through a base-class pointer, the derived destructor runs first, preventing resource leaks.
Which access specifier should be used for data members that must be accessible only within the class and its derived classes?