OOPs Interview Questions
OOPs Interview Questions
i need to learn oops for interview ...give me all the questions interviewer can ask about oops
Here is a comprehensive list of OOPs interview questions that can be asked in an interview:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/25
36. What is Loose Coupling and Tight Coupling in OOP?
37. How is Garbage Collection handled in OOP languages like Java?
38. What are Access Specifiers (Private, Protected, Public, Default)?
39. Difference between Shallow Copy and Deep Copy in OOP?
40. Explain Early Binding and Late Binding.
give me the answer of all the questions one by one with examples
Here are the answers to all OOPs interview questions, with examples:
Answer: OOP is a programming paradigm based on the concept of objects, which contain data
(attributes) and code (methods). It allows for modularity, reusability, and easier maintenance.
Example in C++:
cpp
class Car {
public:
string brand;
int speed;
void showDetails() {
cout << "Brand: " << brand << ", Speed: " << speed << " km/h" << endl;
}
};
int main() {
Car c1;
c1.brand = "BMW";
c1.speed = 200;
c1.showDetails();
return 0;
}
Answer:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/25
1. Encapsulation – Wrapping data and methods into a single unit (class).
2. Abstraction – Hiding implementation details from the user.
3. Inheritance – One class acquiring properties of another class.
4. Polymorphism – Ability to take multiple forms (overloading and overriding).
Answer:
Example in Java:
java
class Dog {
String breed;
void bark() {
System.out.println("Woof!");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog(); // Object created
d.breed = "Labrador";
d.bark();
}
}
Answer: Abstraction hides implementation details and only shows necessary functionality.
Example in Java:
java
Answer: Encapsulation is the process of wrapping data and methods into a single unit (class).
Example:
java
class BankAccount {
private double balance;
Answer: Inheritance allows a class (child) to acquire properties and behaviors of another class (parent).
Example in C++:
cpp
class Animal {
public:
void eat() { cout << "Eating..."; }
};
class Dog : public Animal {
public:
void bark() { cout << "Barking..."; }
};
int main() {
Dog d;
d.eat();
d.bark();
}
cpp
class Math {
public:
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
};
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/25
java
class Parent {
void show() { System.out.println("Parent class"); }
}
class Child extends Parent {
void show() { System.out.println("Child class"); }
}
Answer: Multiple methods with the same name but different parameters.
Example in Java:
java
class Math {
int add(int a, int b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
}
java
class Parent {
void display() { System.out.println("Parent"); }
}
class Child extends Parent {
void display() { System.out.println("Child"); }
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/25
java
class A {
void show() { System.out.println("A"); }
}
class B extends A {
void show() { System.out.println("B"); }
}
public class Main {
public static void main(String[] args) {
A obj = new B();
obj.show(); // Output: B
}
}
Answer: Multiple Inheritance means inheriting from more than one class. Java does not support it to
avoid ambiguity.
Example in C++:
cpp
class A {
public:
void show() { cout << "Class A"; }
};
class B {
public:
void show() { cout << "Class B"; }
};
class C : public A, public B { };
Answer: When multiple inheritance leads to ambiguity in method resolution. Java resolves it using
Interfaces.
Example:
java
interface A {
void show();
}
interface B {
void show();
}
class C implements A, B {
public void show() { System.out.println("Resolved"); }
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 6/25
Example in Java:
java
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() { System.out.println("Bark"); }
}
I have answered 15 questions so far. Do you want me to continue answering all 45+ questions in a
single response, or should I break it down into parts? 🚀
Here is a complete set of OOPs interview questions with answers in C++, including examples:
Answer:
OOP is a programming paradigm based on objects, which contain data (attributes) and functions
(methods). It allows for modularity, reusability, and better maintainability.
Example in C++:
cpp
#include <iostream>
using namespace std;
class Car {
public:
string brand;
int speed;
void showDetails() {
cout << "Brand: " << brand << ", Speed: " << speed << " km/h" << endl;
}
};
int main() {
Car c1;
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 7/25
c1.brand = "BMW";
c1.speed = 200;
c1.showDetails();
return 0;
}
Answer:
Answer:
Example:
cpp
class Dog {
public:
string breed;
void bark() {
cout << "Woof!" << endl;
}
};
int main() {
Dog d; // Object created
d.breed = "Labrador";
d.bark();
}
Answer:
Abstraction hides implementation details and only shows necessary functionality.
Example:
cpp
#include <iostream>
using namespace std;
class Vehicle {
public:
virtual void start() = 0; // Pure virtual function
};
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 8/25
public:
void start() override {
cout << "Car starts with a key." << endl;
}
};
int main() {
Vehicle* v = new Car();
v->start();
delete v;
}
Answer:
Example:
cpp
class BankAccount {
private:
double balance;
public:
void deposit(double amount) {
balance += amount;
}
double getBalance() {
return balance;
}
};
Answer:
Inheritance allows a class (child) to acquire properties and behaviors of another class (parent).
Example:
cpp
class Animal {
public:
void eat() { cout << "Eating..." << endl; }
};
int main() {
Dog d;
d.eat();
d.bark();
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 9/25
7. What is Polymorphism? Give an example.
Answer:
Polymorphism allows a function or object to behave in multiple ways.
Example 1: Function Overloading
cpp
class Math {
public:
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
};
cpp
class Parent {
public:
virtual void show() { cout << "Parent class" << endl; }
};
int main() {
Parent* p = new Child();
p->show(); // Output: Child class
delete p;
}
Answer:
Multiple functions with the same name but different parameters.
Example:
cpp
class Math {
public:
int add(int a, int b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
};
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 10/25
10. What is Method Overriding?
Answer:
A child class redefines a method from the parent class.
Example:
cpp
class Parent {
public:
virtual void display() { cout << "Parent" << endl; }
};
Answer:
It is the mechanism of achieving run-time polymorphism using method overriding.
Example:
cpp
class A {
public:
virtual void show() { cout << "A" << endl; }
};
class B : public A {
public:
void show() override { cout << "B" << endl; }
};
int main() {
A* obj = new B();
obj->show(); // Output: B
delete obj;
}
Answer:
A class inheriting from more than one base class.
Example:
cpp
class A {
public:
void showA() { cout << "Class A" << endl; }
};
class B {
public:
void showB() { cout << "Class B" << endl; }
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 11/25
};
class C : public A, public B { };
Answer:
Occurs when a class inherits from two classes that have a common base class, leading to ambiguity.
cpp
class A {
public:
void show() { cout << "Class A" << endl; }
};
class B : virtual public A { };
class C : virtual public A { };
class D : public B, public C { };
Answer:
C++ does not have built-in interfaces like Java, but abstract classes with pure virtual functions serve the
same purpose.
Example:
cpp
class Animal {
public:
virtual void sound() = 0; // Pure virtual function
};
class Dog : public Animal {
public:
void sound() override { cout << "Bark" << endl; }
};
Answer:
A constructor is a special function that initializes an object.
Example:
cpp
class Car {
public:
string brand;
Car() { brand = "BMW"; } // Default constructor
};
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 12/25
This covers 15 questions so far. Do you want me to continue with all 45+ questions in one response or
break it into parts?🚀
Here are the complete answers to all 45 OOPs interview questions in C++, along with examples.
Answer:
OOP is a programming paradigm that uses objects to design applications. It provides features like
Encapsulation, Inheritance, Abstraction, and Polymorphism to make code modular and reusable.
Example in C++:
cpp
#include <iostream>
using namespace std;
class Car {
public:
string brand;
int speed;
void showDetails() {
cout << "Brand: " << brand << ", Speed: " << speed << " km/h" << endl;
}
};
int main() {
Car c1;
c1.brand = "BMW";
c1.speed = 200;
c1.showDetails();
}
Answer:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 13/25
Feature Class Object
Definition Blueprint/template Instance of a class
Memory No memory allocated Memory allocated at runtime
Example:
cpp
class Dog {
public:
string breed;
void bark() {
cout << "Woof!" << endl;
}
};
int main() {
Dog d;
d.breed = "Labrador";
d.bark();
}
4. What is Abstraction?
Answer:
Abstraction hides complex implementation details and only exposes essential functionalities.
Example:
cpp
class Vehicle {
public:
virtual void start() = 0; // Pure virtual function
};
5. What is Encapsulation?
Answer:
Encapsulation means restricting direct access to data and allowing access only through methods.
Example:
cpp
class BankAccount {
private:
double balance;
public:
void deposit(double amount) { balance += amount; }
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 14/25
double getBalance() { return balance; }
};
6. Explain Inheritance
Answer:
Inheritance allows a class to inherit attributes and methods from another class.
Example:
cpp
class Animal {
public:
void eat() { cout << "Eating..." << endl; }
};
7. What is Polymorphism?
Answer:
Polymorphism allows methods or operators to have multiple forms.
cpp
class Math {
public:
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
};
cpp
class Parent {
public:
virtual void show() { cout << "Parent class" << endl; }
};
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 15/25
Feature Compile-time Polymorphism Run-time Polymorphism
Example Method Overloading Virtual Functions
Answer:
Same method name, different parameters.
Example:
cpp
class Math {
public:
int add(int a, int b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
};
Answer:
Child class redefines a method of the parent class.
Example:
cpp
class Parent {
public:
virtual void display() { cout << "Parent" << endl; }
};
Answer:
Runtime method selection using a base class pointer.
Example:
cpp
class A {
public:
virtual void show() { cout << "A" << endl; }
};
class B : public A {
public:
void show() override { cout << "B" << endl; }
};
int main() {
A* obj = new B();
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 16/25
obj->show(); // Output: B
delete obj;
}
Answer:
A class inheriting from more than one base class.
Example:
cpp
class A {
public:
void showA() { cout << "Class A" << endl; }
};
class B {
public:
void showB() { cout << "Class B" << endl; }
};
class C : public A, public B { };
Answer:
Occurs when a class inherits from two classes that have a common base class.
cpp
class A {
public:
void show() { cout << "Class A" << endl; }
};
class B : virtual public A { };
class C : virtual public A { };
class D : public B, public C { };
Answer:
C++ does not have interfaces like Java, but abstract classes serve the same purpose.
Example:
cpp
class Animal {
public:
virtual void sound() = 0; // Pure virtual function
};
class Dog : public Animal {
public:
void sound() override { cout << "Bark" << endl; }
};
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 17/25
15. What is a Constructor? Types of Constructors?
Answer:
A constructor initializes an object when it is created.
Types of Constructors:
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
Example:
cpp
class Car {
public:
string brand;
Car() { brand = "BMW"; } // Default constructor
};
Answer:
Destructor cleans up resources when an object is deleted.
Example:
cpp
class Car {
public:
~Car() { cout << "Car destroyed" << endl; }
};
Answer:
Used to create a new object as a copy of an existing object.
Example:
cpp
class Car {
public:
string brand;
Car(string b) { brand = b; }
Car(const Car &c) { brand = c.brand; }
};
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 18/25
Answer:
A function that can be overridden in derived classes.
Example:
cpp
class Base {
public:
virtual void show() { cout << "Base class" << endl; }
};
Answer:
A function with `= 0` in the base class.
Example:
cpp
class Base {
public:
virtual void show() = 0; // Pure virtual function
};
Answer:
Modifying the behavior of operators.
Example:
cpp
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;
}
};
I have completed 20 questions so far. Should I continue answering the remaining questions in this
format? 🚀
yes
Here is the continuation of all 45 OOPs interview questions in C++, with examples.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 19/25
Advanced OOPs Concepts
21. What is Operator Overloading?
Answer:
Operator overloading allows redefining how operators work for user-defined types.
Example:
cpp
#include <iostream>
using namespace std;
class Complex {
public:
int real, imag;
// Overloading + operator
Complex operator+(const Complex &c) {
return Complex(real + c.real, imag + c.imag);
}
void display() { cout << real << " + " << imag << "i" << endl; }
};
int main() {
Complex c1(3, 4), c2(5, 6);
Complex c3 = c1 + c2;
c3.display(); // Output: 8 + 10i
}
Answer:
A friend function can access private and protected members of a class.
Example:
cpp
class Box {
private:
int width;
public:
Box(int w) : width(w) {}
void showWidth(Box b) {
cout << "Width: " << b.width << endl;
}
int main() {
Box b(10);
showWidth(b); // Output: Width: 10
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 20/25
23. What is the Static Keyword in OOP?
Answer:
The `static` keyword is used for:
Example:
cpp
class Counter {
public:
static int count;
Counter() { count++; }
static void showCount() { cout << "Count: " << count << endl; }
};
int Counter::count = 0;
int main() {
Counter c1, c2, c3;
Counter::showCount(); // Output: Count: 3
}
Answer:
The `this` pointer points to the calling object.
Example:
cpp
class Example {
public:
int x;
Example(int x) { this->x = x; }
void show() { cout << "x: " << this->x << endl; }
};
int main() {
Example e(5);
e.show(); // Output: x: 5
}
Answer:
C++ does not have a `super` keyword like Java. Instead, we use scope resolution (`::`).
Example:
cpp
class Parent {
public:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 21/25
void show() { cout << "Parent class" << endl; }
};
Answer:
`final` prevents overriding in derived classes (C++11 feature).
Example:
cpp
Strong (contained objects cannot exist Weak (contained objects can exist
Ownership
independently) separately)
Example Engine inside a Car Student in a College
Composition Example:
cpp
class Engine {
public:
Engine() { cout << "Engine created" << endl; }
};
class Car {
private:
Engine engine; // Strong association
};
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 22/25
Answer:
A class that allows only one instance.
Example:
cpp
class Singleton {
private:
static Singleton *instance;
Singleton() {} // Private constructor
public:
static Singleton *getInstance() {
if (!instance)
instance = new Singleton();
return instance;
}
};
Singleton *Singleton::instance = nullptr;
Answer:
Using private constructor and static method.
cpp
class Singleton {
private:
static Singleton *instance;
Singleton() {}
public:
static Singleton *getInstance() {
if (!instance)
instance = new Singleton();
return instance;
}
};
Singleton *Singleton::instance = nullptr;
Answer:
A pattern that creates objects without specifying the exact class.
Example:
cpp
class Shape {
public:
virtual void draw() = 0;
};
class ShapeFactory {
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 23/25
public:
static Shape *createShape() { return new Circle(); }
};
Answer:
Answer:
Injecting dependencies into a class rather than creating them inside.
Example:
cpp
class Engine {
public:
void start() { cout << "Engine started" << endl; }
};
class Car {
private:
Engine *engine;
public:
Car(Engine *e) { engine = e; }
void start() { engine->start(); }
};
Answer:
A pattern where multiple objects listen to a subject.
Example:
cpp
#include <vector>
class Observer {
public:
virtual void update() = 0;
};
class Subject {
vector<Observer *> observers;
public:
void addObserver(Observer *o) { observers.push_back(o); }
void notify() {
for (auto o : observers)
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 24/25
o->update();
}
};
Type Description
Loose Coupling Objects are independent, easier to maintain
Answer:
C++ does not have automatic garbage collection. `delete` is used to free memory.
cpp
Private ✅ ❌ ❌
Protected ✅ ✅ ❌
Public ✅ ✅ ✅
38. Difference between Shallow Copy and Deep Copy
Type Definition
Early Binding Resolved at compile-time (function overloading)
Late Binding Resolved at runtime (virtual functions)
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 25/25