Home Subjects C++ Programming OOP Concepts

C++ Programming
OOP Concepts

Object oriented C++ for GATE and placement

23 Q 1 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 11–20 of 23
Topics in C++ Programming
All OOP Concepts 100
Q.11 Hard OOP Concepts
Consider the code: class A { int x; }; class B : private A { }; class C : public B { };
Can class C access x?
A Yes, directly
B No, because x is private to A
C Yes, through B
D No, because B inherits privately from A
Correct Answer:  D. No, because B inherits privately from A
EXPLANATION

When B inherits privately from A, the protected and public members of A become private in B. C cannot access them because they are not accessible through the inheritance chain.

Test
Q.12 Hard OOP Concepts
What happens when you try to delete a derived class object through a base class pointer without a virtual destructor?
A Only base class destructor is called
B Both destructors are called
C Memory leak occurs in derived class
D Compilation error
Correct Answer:  C. Memory leak occurs in derived class
EXPLANATION

Without a virtual destructor, only the base class destructor is called, leading to incomplete cleanup of derived class resources and potential memory leaks.

Test
Q.13 Hard OOP Concepts
What is the primary advantage of using interfaces (abstract classes with pure virtual functions) in C++?
A They make code run faster
B They define a contract that derived classes must follow, ensuring consistency and flexibility
C They automatically manage memory
D They prevent inheritance from occurring
Correct Answer:  B. They define a contract that derived classes must follow, ensuring consistency and flexibility
EXPLANATION

Interfaces (abstract classes) define a contract specifying what methods derived classes must implement, promoting loose coupling and design flexibility.

Test
Q.14 Hard OOP Concepts
In C++, what does the 'mutable' keyword do when applied to a class member?
A Allows the member to be modified even in a const object
B Prevents the member from being accessed outside the class
C Makes the member inherit from parent class
D Converts the member to a static variable
Correct Answer:  A. Allows the member to be modified even in a const object
EXPLANATION

The 'mutable' keyword allows a member variable to be modified even within a const member function or when the object itself is const.

Test
Q.15 Hard OOP Concepts
What is a virtual destructor and why is it important?
A A destructor that can be overridden; important for polymorphic classes to ensure derived class destructors are called
B A destructor that doesn't actually delete anything
C A destructor that is inherited from base class
D A destructor that is called before constructor
Correct Answer:  A. A destructor that can be overridden; important for polymorphic classes to ensure derived class destructors are called
EXPLANATION

A virtual destructor ensures that when deleting a base class pointer pointing to a derived object, the derived class destructor is called first, preventing resource leaks.

Test
Q.16 Hard OOP Concepts
What is the difference between shallow copy and deep copy in C++?
A Shallow copy copies values, deep copy copies references
B Shallow copy copies references, deep copy copies all data including dynamically allocated memory
C They are the same thing
D Shallow copy is faster but unsafe, deep copy is slower but redundant
Correct Answer:  B. Shallow copy copies references, deep copy copies all data including dynamically allocated memory
EXPLANATION

Shallow copy only copies the pointer references, while deep copy allocates new memory and copies the actual data, preventing dangling pointers.

Test
Q.17 Hard OOP Concepts
Which of the following best describes the Liskov Substitution Principle (LSP)?
A Objects of derived classes must be substitutable for objects of base classes
B Derived classes must override all base class methods
C Base classes cannot have virtual functions
D Inheritance should be avoided in large programs
Correct Answer:  A. Objects of derived classes must be substitutable for objects of base classes
EXPLANATION

LSP states that objects of a derived class should be able to replace objects of the base class without breaking the program. This ensures predictable polymorphic behavior.

Test
Q.18 Hard OOP Concepts
Which of the following is NOT a characteristic of a good object-oriented design?
A High cohesion within classes
B Low coupling between classes
C Maximum code duplication to ensure flexibility
D Proper encapsulation of data
Correct Answer:  C. Maximum code duplication to ensure flexibility
EXPLANATION

Good OOP design aims to minimize code duplication through inheritance and composition, not maximize it. High cohesion, low coupling, and proper encapsulation are fundamental principles.

Test
Q.19 Hard OOP Concepts
How is the Diamond Problem solved in C++ using virtual inheritance?
A By preventing multiple inheritance completely
B By creating multiple copies of the base class
C By ensuring only one copy of the base class exists in the inheritance hierarchy
D By making all functions virtual
Correct Answer:  C. By ensuring only one copy of the base class exists in the inheritance hierarchy
EXPLANATION

Virtual inheritance ensures that when a class appears multiple times in an inheritance hierarchy, only one copy of it is included, resolving ambiguity and the Diamond Problem.

Test
Q.20 Hard OOP Concepts
Consider the following code:
class A { public: virtual ~A() {} };
class B : public A { public: ~B() {} };
int main() { A* ptr = new B(); delete ptr; }
What is the significance of virtual destructor here?
A It prevents memory leaks by calling derived class destructor
B It allows multiple inheritance
C It enables operator overloading
D It makes the class abstract
Correct Answer:  A. It prevents memory leaks by calling derived class destructor
EXPLANATION

A virtual destructor ensures that when a derived class object is deleted through a base class pointer, the derived class destructor is called first, preventing memory leaks and ensuring proper cleanup.

Test
IGET
IGET AI
Online · Exam prep assistant
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips