OOP
OOP
Answer:
OOP (Object-Oriented Programming) is a programming paradigm based on objects that
contain data (attributes) and methods (functions) to operate on the data. It helps organize
code into reusable and manageable components.
Answer:
The four main principles of OOP are:
1. Encapsulation – Hiding internal details and exposing only the necessary parts.
3. Inheritance – Reusing existing code by deriving new classes from base classes.
3. What is a class?
Answer:
A class is a blueprint for creating objects. It defines attributes (data) and behaviors
(methods) that the object will have.
4. What is an object?
Answer:
An object is an instance of a class that contains actual values for the attributes and can
perform actions defined by the methods.
5. What is encapsulation?
Answer:
Encapsulation is the practice of wrapping data (variables) and methods (functions) into a
single unit (class) and restricting direct access to them to protect data integrity.
6. What is abstraction?
Answer:
Abstraction means hiding complex implementation details and showing only the necessary
features to the user.
7. What is inheritance?
Answer:
Inheritance allows a class to inherit properties and behaviors from another class, promoting
code reuse.
8. What is polymorphism?
Answer:
Polymorphism allows a function or method to have multiple forms, such as method
overloading (same function name with different parameters) and method overriding
(redefining a function in a subclass).
Answer:
Answer:
Method overloading allows multiple methods with the same name but different parameters
within the same class.
12. What is the difference between method overloading and method overriding?
Answer:
Answer:
Class Object
Answer:
Constructors are special methods that initialize an object when it is created.
Answer:
Destructors are methods that clean up resources when an object is destroyed.
Answer:
An abstract class cannot be instantiated and may contain abstract methods (without
implementation).
17. What is an interface?
Answer:
An interface is a contract that defines a set of methods that must be implemented by any
class that adopts it.
Answer:
Answer:
Multiple inheritance allows a class to inherit from more than one base class.
Answer:
A static method belongs to the class rather than an instance and can be called without
creating an object.
Answer:
A static variable belongs to the class and is shared among all objects.
Answer:
The "this" keyword refers to the current instance of the class.
23. What is the "super" keyword?
Answer:
The "super" keyword is used to call a parent class constructor or method.
Answer:
Dynamic method dispatch (runtime polymorphism) allows calling overridden methods based
on the object's actual type at runtime.
Answer:
Coupling refers to the degree of dependency between classes. Low coupling is preferred for
better maintainability.
Answer:
Cohesion refers to how well a class focuses on a single responsibility.
Answer:
Association represents a relationship between objects where they can exist independently
but work together.
Answer:
Aggregation is a "has-a" relationship where an object contains another object.
Answer:
Composition is a stronger form of aggregation where the contained object cannot exist
without the parent object.
30. What is dependency injection?
Answer:
Dependency Injection is a design pattern where dependencies are provided from outside the
class rather than being created inside.
Answer:
A singleton class allows only one instance of the class to exist.
Answer:
Access modifiers define the visibility of class members (public, private, protected).
Answer:
An inner class is a class defined within another class.
Answer:
An immutable object cannot be changed after it is created.
Answer:
A shallow copy copies references of objects, not the actual objects.
Answer:
A deep copy creates a new copy of the actual objects.
37. What is the difference between "==" and "equals()" in Java?
Answer:
"==" compares memory addresses, while "equals()" compares content.
Answer:
A design pattern is a general solution to a common problem in software design.
Answer:
SOLID stands for:
1. Single Responsibility
2. Open/Closed Principle
3. Liskov Substitution
4. Interface Segregation
5. Dependency Inversion
Here are more OOP interview questions along with simple answers:
Answer:
Answer:
A friend function allows access to private and protected members of a class without being a
member of that class.
42. What is the difference between public, private, and protected access specifiers?
Answer:
Private Yes No No
Answer:
Operator overloading allows you to redefine the behavior of operators (e.g., +, -) for user-
defined types in languages like C++.
44. What is the difference between early binding and late binding?
Answer:
• Early Binding: Method call is resolved at compile time (e.g., method overloading).
• Late Binding: Method call is resolved at runtime (e.g., method overriding using
virtual functions).
45. What is the difference between deep copy and shallow copy?
Answer:
Answer:
Answer:
A subclass should be able to replace a parent class without affecting the program's
correctness.
49. What is the difference between an interface and an abstract class in Java?
Answer:
• Abstract Class: Partial abstraction, can have implemented methods and constructors.
Answer:
Association is a relationship between two independent classes, where objects can interact
with each other but are not dependent.
Answer:
Duck typing is a concept in dynamically typed languages (like Python), where an object's
methods determine its type rather than inheritance.
Answer:
Multiple dispatch means a method can be selected based on the runtime types of multiple
arguments (common in Python, CLOS).
53. What is the difference between a constructor and a method?
Answer:
Answer:
Yes, a private constructor is used in singleton design patterns to restrict object creation.
Answer:
Function overriding allows a subclass to provide its own implementation of a method
already defined in its parent class.
Answer:
Virtual functions allow dynamic binding, meaning the function call is resolved at runtime
based on the actual object type.
Answer:
An abstract method is a method without implementation, meant to be overridden by
subclasses.
Answer:
An object's lifetime refers to the period between its creation (instantiation) and its
destruction (garbage collection or manual deallocation).
Answer:
• "Is-a": Represents inheritance (e.g., Dog is an Animal).
Answer:
If a subclass defines a static method with the same signature as in the parent class, it hides
the parent method rather than overriding it.
Answer:
A namespace is used to group related classes, functions, and variables to avoid name
conflicts.
Answer:
1. Code reusability.
2. Improved maintainability.
4. Scalability.
Answer:
Answer:
A function declared in an abstract class with = 0 in C++ is a pure virtual function, forcing
derived classes to implement it.
65. What is a virtual destructor in C++?
Answer:
A virtual destructor ensures proper cleanup of resources in an inheritance hierarchy.
Answer:
It occurs in multiple inheritance when two parent classes share a common base class,
leading to ambiguity.
Answer:
Answer:
Methods used to access (get) and modify (set) private variables of a class.
Answer:
The adapter pattern allows incompatible interfaces to work together by acting as a bridge.
Answer:
Object cloning creates a duplicate object with the same properties.
Answer:
A class should be open for extension but closed for modification.
72. What is cohesion and why is it important?
Answer:
Cohesion measures how well a class focuses on a single responsibility. High cohesion
improves code maintainability.
Answer:
Loose coupling means reducing dependencies between classes to make the system easier to
change.
Answer:
No, constructors do not return values; they only initialize objects.
Answer: