Advanced OOPs Interview Preparation in C++
1. What is Object-Oriented Programming (OOP)? Why is it important?
OOP is a paradigm centered around objects, which encapsulate data and behavior. It helps manage
complexity, promotes reuse, and models real-world systems effectively. Core principles include inheritance,
polymorphism, encapsulation, and abstraction.
2. Explain Classes and Objects in C++.
A class is a blueprint defining data and methods. An object is an instance of a class. Example:
class Car {
public:
std::string model;
int year;
void displayInfo() {
std::cout << "Model: " << model << ", Year: " << year << std::endl;
};
int main() {
Car myCar;
myCar.model = "Toyota";
myCar.year = 2023;
myCar.displayInfo();
3. What is Encapsulation? How is it achieved in C++?
Encapsulation means binding data and functions into a class and hiding internal details using access
specifiers:
- private: accessible only within the class
Advanced OOPs Interview Preparation in C++
- protected: accessible in class and derived
- public: accessible anywhere
4. What is Abstraction? How is it achieved in C++?
Abstraction shows only essential details. Achieved via abstract classes and pure virtual functions:
class Shape {
public:
virtual void draw() = 0;
};
5. What is Inheritance? Explain its types in C++.
Inheritance allows a class to use properties of another. Types:
- Single
- Multiple
- Multilevel
- Hierarchical
- Hybrid
6. What is Polymorphism? Explain Compile-time and Run-time Polymorphism in C++.
Polymorphism means same interface with different behavior.
- Compile-time: Function/Operator Overloading
- Run-time: Virtual functions and overriding
7. What is a Constructor? What are its types in C++?
Constructors initialize objects. Types:
- Default
- Parameterized
- Copy Constructor
- Move Constructor
Advanced OOPs Interview Preparation in C++
8. What is a Destructor? Can it be overloaded?
Destructor cleans up resources. Syntax: ~ClassName().
It can't be overloaded.
9. Explain the 'this' pointer in C++.
'this' is an implicit pointer to the current object, useful for resolving variable name conflicts and chaining
method calls.
10. What is a Friend Function and Friend Class in C++?
Friend function can access private members. Declared using 'friend'. Friend class allows all its members to
access private/protected data.
11. What is the difference between struct and class in C++?
struct: default public
class: default private
Both support member functions and access specifiers.
12. What are Abstract Classes and Pure Virtual Functions? When are they used?
Abstract class has at least one pure virtual function (=0). Used to define interfaces and enable polymorphism.
13. What is a Virtual Destructor? Why is it important?
A virtual destructor ensures derived class destructors are called when deleting via base class pointer,
preventing memory leaks.
14. What is the Diamond Problem in C++? How can it be resolved?
Occurs in multiple inheritance when same base class is inherited more than once. Solved using virtual
inheritance.
15. Differentiate between Method Overloading and Method Overriding.
Overloading: Same name, different signatures in same class.
Overriding: Redefining base method in derived class. Requires 'virtual'.
Advanced OOPs Interview Preparation in C++
16. What are Access Specifiers? Explain public, private, and protected.
public: accessible anywhere
private: within class only
protected: class + derived
17. What is new and delete in C++? How do they differ from malloc and free?
'new' allocates and calls constructor. 'delete' deallocates and calls destructor. malloc/free don't call
constructors/destructors.
18. What is a Copy Constructor and when is it called?
Used to create a new object as a copy of an existing one. Called during pass-by-value, return by value, etc.
19. What is Shallow Copy and Deep Copy?
Shallow copy copies pointers; both point to same memory. Deep copy allocates separate memory and copies
data.
20. What is a pure virtual function in C++?
A function declared with '= 0' in base class. Makes class abstract. Derived classes must override it.