Q1. What is the difference between abstraction and encapsulation?
Answer:
-
Abstraction → Hides implementation details and shows only the essential features (e.g., abstract classes, interfaces).
-
Encapsulation → Hides data by binding it with methods inside a class (e.g., private fields with getters/setters).
👉 Abstraction is about what a class does. Encapsulation is about how it hides data internally.
Q2. Why do we need a virtual destructor in C++?
Answer:
-
If a base class pointer points to a derived object and we delete it, without a virtual destructor only the base destructor runs.
-
This causes memory/resource leaks.
-
With a virtual destructor, both base and derived destructors run.
Q3. Can constructors be virtual in C++? Why not?
Answer:
-
No, constructors cannot be virtual.
-
Because at construction time, the object’s vtable pointer (
vptr) is not set up yet. -
Virtual mechanism works only after an object is created.
Q4. What is the diamond problem in C++? How is it solved?
Answer:
-
Occurs when a class inherits from two classes that both inherit from the same base class.
-
Causes ambiguity (two copies of the base class).
-
Solved using virtual inheritance.
Example:
Copyclass A {};
class B : virtual public A {};
class C : virtual public A {};
class D : public B, public C {}; // only one A inside D
Q5. Difference between method overloading and method overriding.
Answer:
-
Overloading (compile-time polymorphism): Same method name, different parameter list.
-
Overriding (runtime polymorphism): Subclass provides its own implementation of a base class method with the same signature.
Coding Questions
Q6. Explain deep copy vs shallow copy with a code example.
Answer:
Copyclass Student {
char* name;
public:
Student(const char* n) { // constructor
name = new char[strlen(n)+1];
strcpy(name, n);
}
// Deep copy constructor
Student(const Student& s) {
name = new char[strlen(s.name)+1];
strcpy(name, s.name);
}
~Student() { delete[] name; } // destructor
void show() { cout << name << endl; }
};
-
Shallow copy: just copies the pointer, both objects share the same memory.
-
Deep copy: allocates new memory and copies data independently.
Q7. Given the following code, what will be the output?
Copyclass Base {
public:
virtual void show() { cout << "Base\n"; }
};
class Derived : public Base {
public:
void show() override { cout << "Derived\n"; }
};
int main() {
Base* b = new Derived();
b->show();
delete b;
}
Answer:
CopyDerived
Because show() is virtual, so dynamic dispatch calls Derived::show() at runtime.
Q8. What happens in this code?
Copyclass Base {
public:
~Base() { cout << "Base Destructor\n"; }
};
class Derived : public Base {
public:
~Derived() { cout << "Derived Destructor\n"; }
};
int main() {
Base* obj = new Derived();
delete obj;
}
Answer:
CopyBase Destructor
-
Deriveddestructor is never called → memory leak. -
Fix: declare base destructor as
virtual ~Base().
Q9. Can you write a C++ interface for PaymentMethod where different classes implement it?
Answer:
Copyclass PaymentMethod {
public:
virtual void pay(int amount) = 0; // pure virtual
};
class CreditCard : public PaymentMethod {
public:
void pay(int amount) override { cout << "Paid " << amount << " with Credit Card\n"; }
};
class PayPal : public PaymentMethod {
public:
void pay(int amount) override { cout << "Paid " << amount << " with PayPal\n"; }
};
int main() {
PaymentMethod* p1 = new CreditCard();
p1->pay(100);
PaymentMethod* p2 = new PayPal();
p2->pay(200);
}
👉 Demonstrates interface via abstract class + runtime polymorphism.
Q10. Predict the output:
Copyclass A {
public:
virtual void print() { cout << "A"; }
};
class B : public A {
public:
void print() override { cout << "B"; }
};
class C : public B {
public:
void print() override { cout << "C"; }
};
int main() {
A* obj = new C();
obj->print();
}
Answer:
CopyC
Because obj is an A*, but points to a C object. Virtual function dispatch calls C::print().
Higher-Level Design / Theory Questions
Q11. What are SOLID principles in OOP?
Answer:
-
S → Single Responsibility Principle
-
O → Open/Closed Principle
-
L → Liskov Substitution Principle
-
I → Interface Segregation Principle
-
D → Dependency Inversion Principle
Q12. Difference between abstract class and interface in C++.
Answer:
-
Abstract class → can have data + concrete methods + pure virtual methods.
-
Interface (simulated via abstract class with only pure virtual methods) → only declares pure virtual functions, no implementation.