What is the output of the following code?
class A {
public:
A() { cout << "A "; }
~A() { cout << "~A "; }
};
class B : public A {
public:
B() { cout << "B "; }
~B() { cout << "~B "; }
};
int main() {
B obj;
return 0;
}
Answer: A
Constructors are called from base to derived (A then B), while destructors are called from derived to base (~B then ~A).
Q.2Hard
Consider the following code:
class Base {
protected:
int x = 10;
};
class Derived : private Base {
public:
void display() { cout << x; }
};
int main() {
Derived d;
d.display();
}
Answer: A
Even with private inheritance, protected members of Base are accessible within Derived class methods. The display() function can access x and will print 10.
Q.3Hard
What is the output of operator overloading in the following context?
class Complex {
public:
int real, imag;
Complex operator+(const Complex &c) {
Complex temp;
temp.real = real + c.real;
temp.imag = imag + c.imag;
return temp;
}
};
Complex c1, c2, c3;
c1 = {1, 2}; c2 = {3, 4};
c3 = c1 + c2;
Answer: A
The + operator is overloaded to add two Complex numbers. c1 + c2 calls the overloaded operator+, resulting in real=1+3=4 and imag=2+4=6.
Q.4Hard
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?
Answer: A
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.
Q.5Hard
How is the Diamond Problem solved in C++ using virtual inheritance?
Answer: C
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.
Advertisement
Q.6Hard
Which of the following is NOT a characteristic of a good object-oriented design?
Answer: C
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.
Q.7Hard
Which of the following best describes the Liskov Substitution Principle (LSP)?
Answer: A
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.
Q.8Hard
What is the difference between shallow copy and deep copy in C++?
Answer: B
Shallow copy only copies the pointer references, while deep copy allocates new memory and copies the actual data, preventing dangling pointers.
Q.9Hard
What is a virtual destructor and why is it important?
Answer: A
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.
Q.10Hard
In C++, what does the 'mutable' keyword do when applied to a class member?
Answer: A
The 'mutable' keyword allows a member variable to be modified even within a const member function or when the object itself is const.
Q.11Hard
What is the primary advantage of using interfaces (abstract classes with pure virtual functions) in C++?
Answer: B
Interfaces (abstract classes) define a contract specifying what methods derived classes must implement, promoting loose coupling and design flexibility.
Q.12Hard
What happens when you try to delete a derived class object through a base class pointer without a virtual destructor?
Answer: C
Without a virtual destructor, only the base class destructor is called, leading to incomplete cleanup of derived class resources and potential memory leaks.
Q.13Hard
Consider the code: class A { int x; }; class B : private A { }; class C : public B { };
Can class C access x?
Answer: D
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.
Q.14Hard
In C++, what is the result of dynamic_cast when it fails on a pointer?
Answer: B
When dynamic_cast fails on a pointer, it returns nullptr. For references, it throws a std::bad_cast exception.
Q.15Hard
What will be printed by this code?
class A { public: virtual void print() { cout << "A"; } };
class B : public A { public: void print() { cout << "B"; } };
int main() { A *arr[2]; arr[0] = new A(); arr[1] = new B(); arr[0]->print(); arr[1]->print(); }
Answer: A
With virtual functions and dynamic binding, arr[0]->print() calls A's print (outputs 'A'), and arr[1]->print() calls B's overridden print (outputs 'B'). Result: 'AB'.
Q.16Hard
In a multiple inheritance scenario with diamond problem, how does C++ resolve ambiguity?
Answer: B
The diamond problem occurs when a derived class inherits from two classes that have a common base. Virtual inheritance ensures only one copy of the base class is created.
Q.17Hard
Consider a scenario where class C inherits from both class A and class B, and both A and B have a method foo(). Which statement correctly resolves the ambiguity?
Answer: B
In multiple inheritance, if both base classes have the same method, the derived class must either override it or use the scope resolution operator (A::foo() or B::foo()) to specify which one to call.
Q.18Hard
What is the relationship between references and pointers in C++ OOP context?
Answer: D
Both references and pointers can be used for polymorphism in C++. References are generally safer (no null references, cannot be reassigned) while pointers are more flexible (can be null, reassigned).
Q.19Hard
Consider the following code: class A { public: virtual ~A() {} }; class B : public A {}; int main() { A *ptr = new B(); delete ptr; }. What does the virtual destructor ensure?
Answer: B
A virtual destructor ensures that when a derived class object is deleted through a base class pointer, the derived class destructor is called first, followed by the base class destructor, properly cleaning up all resources.
Q.20Hard
What is the key difference between shallow copy and deep copy in the context of copy constructors?
Answer: B
Shallow copy performs a bitwise copy of members, which can be problematic for pointers (both objects point to same memory). Deep copy allocates new memory for dynamic members, ensuring independent objects.