Great!
I’ll provide well-structured, interview-style answers to all your Object-Oriented
Programming (OOP) questions.
Since the list is long, I’ll start with the first section (Basic OOP Concepts). After that, I’ll
continue with the next sections one by one.
🧠 1. Basic OOP Concepts
1. What is the need for OOPs?
OOP addresses problems of procedural programming like code duplication, poor
modularity, and scalability. It helps organize complex code using real-world entities,
making software more maintainable and reusable.
2. What are some major Object Oriented Programming languages?
   ● Java
   ● C++
   ● Python
   ● C#
   ● Ruby
   ● Kotlin
   ● Swift
3. What are some other programming paradigms other than OOPs?
   ● Procedural Programming (e.g., C)
   ● Functional Programming (e.g., Haskell, Scala)
   ● Logic Programming (e.g., Prolog)
   ● Event-driven Programming (e.g., JavaScript in browsers)
4. What is meant by Structured Programming?
It’s a subset of procedural programming where programs are written using structures like
sequence, selection (if-else), and loops, aiming for readable and maintainable code.
5. What are the main features of OOPs?
   ● Encapsulation
   ● Abstraction
   ● Inheritance
   ● Polymorphism
6. What are some advantages of using OOPs?
   ● Code reuse via inheritance
   ● Better modularity
   ● Easier troubleshooting
   ● Real-world modeling
   ● Extensibility
7. Why is OOPs so popular?
OOP allows real-world modeling, modular code, better maintainability, and reuse of code,
making it ideal for large, complex applications.
8. What is meant by the term OOPs?
OOP is a programming paradigm where programs are organized around objects rather
than functions or logic.
9. What are the four main principles of OOP?
   1. Encapsulation
   2. Abstraction
   3. Inheritance
   4. Polymorphism
10. Explain the difference between a class and an object.
   ● A class is a blueprint or template (e.g., Car).
   ● An object is an instance of the class (e.g., HondaCity).
11. What is encapsulation? Provide an example.
Encapsulation means bundling data and methods in a single unit and restricting direct
access.
Example (Java):
java
CopyEdit
class Student {
     private int age;
     public void setAge(int age) { this.age = age; }
     public int getAge() { return age; }
}
12. What is abstraction? How is it different from encapsulation?
Abstraction hides complex logic and exposes only the necessary parts.
Encapsulation hides internal data using access modifiers.
   ● Abstraction = Hiding implementation
   ● Encapsulation = Hiding data
13. What is polymorphism? Explain with an example.
Polymorphism means the ability of a single method or function to behave differently
based on context.
Example:
java
CopyEdit
class Animal {
     void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
     void sound() { System.out.println("Bark"); }
}
14. What is inheritance? How does it promote code reusability?
Inheritance allows a class to derive properties/methods from another.
 It promotes code reuse by avoiding redefinition of common functionalities.
15. What are access specifiers in OOP?
They control visibility:
   ● public: Accessible everywhere
   ● private: Accessible only within class
   ● protected: Accessible within class and subclasses
16. What do you understand by OOP?
OOP is a method of programming where code is grouped into objects that combine data
and behavior, enabling modular, reusable, and maintainable code.
17. Name any seven widely used OOP languages.
   ● Java
   ● C++
   ● Python
   ● C#
   ● Ruby
   ● Kotlin
   ● Swift
18. What is the purpose of using OOPs concepts?
To build flexible, scalable, maintainable software by organizing code into reusable and
logical modules (objects).
19. What are the four main features of OOPs?
   1. Encapsulation
   2. Abstraction
   3. Inheritance
   4. Polymorphism
20. What are the advantages and disadvantages of OOP?
Advantages:
   ● Modular code
   ● Code reuse
   ● Scalability
   ● Better debugging
Disadvantages:
   ● Steeper learning curve
   ● More memory usage
   ● Not always ideal for small/simple tasks
21. What are the limitations of OOPs?
    ● Can be overkill for small programs
    ● Requires more design effort
    ● Performance overhead due to abstraction
22. What are the differences between object-oriented programming and structural
programming?
                 Feature              Structural                  OOP
                                    Programming
            Structure          Based on functions        Based on objects
            Code Reuse         Limited                   High via inheritance
            Data Handling      Publicly accessible       Secured via
                                                         encapsulation
            Example            C, Pascal                 Java, C++, Python
            Languages
23. What do you understand by pure object-oriented language? Why is Java not a pure
object-oriented programming language?
A pure OOP language treats everything as an object.
Java is not pure OOP because it supports primitive types (int, char, etc.), which are
not objects.
Let me know if you’d like me to continue with the next section: “Classes and Objects”.
You said:
yes continue
ChatGPT said:
Great! Let’s continue with the next section of OOP interview
questions:
🧱 2. Classes and Objects
1. What is a class?
A class is a blueprint or template for creating objects. It defines the attributes (data
members) and behaviors (methods) that the objects created from the class will have.
2. What is an object?
An object is an instance of a class. It contains real values instead of abstract definitions
and can use the class's methods and properties.
3. How do you define a class in Java?
java
CopyEdit
class Car {
     String color;
     void startEngine() {
        System.out.println("Engine started");
     }
}
4. How do you create an object from a class?
java
CopyEdit
Car myCar = new Car(); // myCar is an object of Car
5. What is the difference between a constructor and a method?
          Feature                   Constructor                       Method
       Purpose          Initializes a new object             Performs a specific
                                                             action
       Name             Same as class name                   Any valid name
       Return           No return type, not even void        Must have a return type
       Type
       Call             Called automatically on object       Called manually
                        creation
6. What is a destructor? When is it called?
In languages like C++, a destructor is used to clean up resources and is called
automatically when an object goes out of scope.
Java doesn't have destructors but uses a garbage collector and the finalize()
method (deprecated in recent versions).
7. Can a constructor be private? Why or why not?
Yes. A private constructor is used in the Singleton Design Pattern to restrict
instantiation of the class from outside.
java
CopyEdit
class Singleton {
     private static Singleton instance = new Singleton();
     private Singleton() {} // private constructor
     public static Singleton getInstance() {
        return instance;
     }
}
8. How much memory does a class occupy?
A class itself doesn't occupy memory until an object is created. Memory is allocated only
when an object is instantiated.
9. Is it always necessary to create objects from a class?
No. You can use static methods and variables without creating objects. Also, in some
design patterns (e.g., utility/helper classes), objects are never created.
10. What do you understand by class and object? Also, give an example.
A class defines a structure, and an object is an actual entity created based on that class.
Example:
java
CopyEdit
class Student {
     String name;
     void study() {
        System.out.println(name + " is studying");
     }
}
Student s = new Student(); // object
s.name = "Abhi";
s.study(); // Output: Abhi is studying
11. What are the differences between class and object?
                   Aspect           Class                      Object
                Definitio    Blueprint/template     Instance of a class
                n
                Memory       No memory              Occupies memory when
                             allocation             created
                Access       Cannot use directly    Can use class members
12. What are the key differences between class and structure?
                   Feature                  Class             Structure (in C/C++/C#)
            Default Access       Private                      Public
            Inheritance          Yes                          Limited or no support
            Support
            Type                 Reference type (in           Value type (in C#/C++)
                                 Java/C#)
            Memory               Heap                         Stack
            Allocation
✅ Let me know if I should continue with the next section: Encapsulation.
You said:
continue
ChatGPT said:
Perfect! Let’s move on to the next section:
🔒 3. Encapsulation
1. What is encapsulation?
Encapsulation is the bundling of data and methods that operate on that data into a
single unit (typically a class) and restricting direct access to some of the object’s
components to ensure data integrity.
      ✅ It’s achieved using access modifiers like private, public, and
      protected.
2. How does encapsulation improve code maintainability?
    ● Reduces interdependence of code
    ● Protects internal state of objects
    ● Allows controlled access through getters/setters
    ● Makes code more modular and easier to debug and maintain
3. What are getters and setters?
Getters retrieve private field values, and setters modify them.
Example:
java
CopyEdit
class Student {
     private int age;
    public int getAge() {
       return age;
    }
    public void setAge(int a) {
       if (a > 0) age = a;
    }
}
4. What is the difference between public, private, and protected access modifiers?
Modifier    Accessible Within           Within           Subclass (other             Outside
                 Class                 Package             package)                  Package
public     ✅                       ✅                 ✅                         ✅
privat      ✅                        ❌               ❌                       ❌
e
protec      ✅                        ✅               ✅                       ❌
ted
(default)   ✅                        ✅               ❌                       ❌
5. What is the concept of access specifiers, and when should we use these?
Access specifiers define the visibility of class members (fields and methods). Use them to:
    ● Protect sensitive data (use private)
    ● Allow read-only or write-only access (using getter or setter)
    ● Expose methods to external code (public)
    ● Allow subclass access but not others (protected)
Would you like to continue with the next section: 4. Abstraction?
You said:
continue
ChatGPT said:
Awesome! Let’s move forward with:
🎭 4. Abstraction
1. What is Abstraction?
Abstraction means hiding internal implementation details and exposing only the
essential features. It simplifies complexity and helps focus on what an object does
instead of how it does it.
2. How do abstract classes differ from interfaces?
    Feature               Abstract Class                        Interface
Methods          Can have both abstract and     Only abstract methods (until Java 7);
                 concrete methods               default/static allowed from Java 8
Variables        Can have instance variables    Only constants (public static final)
Inheritance      Supports single inheritance    Supports multiple inheritance
Constructor Can have constructors               Cannot have constructors
s
3. Can we create an instance of an abstract class?
No, an abstract class cannot be instantiated. You must extend it and implement its
abstract methods in a subclass.
4. What happens if a subclass does not implement all abstract methods of a parent class?
The subclass must also be declared abstract. Otherwise, the code will not compile.
5. How is abstraction implemented in different programming languages?
      ● Java/C++: Through abstract classes and interfaces
      ● Python: Using abstract base classes (abc module)
      ● C#: Via abstract classes and interfaces
      ● JavaScript/TypeScript: Via interfaces and base classes
6. How is data abstraction accomplished?
By:
      ● Using access modifiers (private, public, etc.)
      ● Creating interfaces or abstract classes
      ● Providing only necessary methods and hiding inner logic
7. What is an abstract class?
An abstract class is a class that cannot be instantiated and may contain abstract
methods (methods without implementation) which must be implemented by subclasses.
java
CopyEdit
abstract class Animal {
     abstract void makeSound();
}
8. How is an abstract class different from an interface?
To summarize:
    ● Abstract class can have concrete methods, interface could not (until Java 8).
    ● You can extend only one abstract class, but implement multiple interfaces.
    ● Interfaces represent a contract, while abstract classes provide partial
       implementation.
9. What are the characteristics of an abstract class?
    ● Cannot be instantiated
    ● May have constructors
    ● Can contain both abstract and non-abstract methods
    ● Can include fields and access modifiers
    ● Supports inheritance
Ready to continue with 5. Inheritance?
You said:
continue
ChatGPT said:
Great! Let's continue with the next major concept in OOP:
🧬 5. Inheritance
1. What is meant by Inheritance?
Inheritance is a mechanism where a child class (subclass) acquires properties and
behaviors (methods and fields) from a parent class (superclass). It promotes code
reusability and hierarchical relationships.
2. What is the difference between single and multiple inheritance?
        Type                                       Description
 Single               A subclass inherits from one superclass.
 Inheritance
 Multiple             A subclass inherits from more than one class (C++, not supported
 Inheritance          in Java via classes but possible via interfaces).
3. What are the various types of inheritance?
   ● Single Inheritance
   ● Multilevel Inheritance
   ● Hierarchical Inheritance
   ● Multiple Inheritance (via interfaces in Java)
   ● Hybrid Inheritance
4. What is a subclass?
A subclass (child class) is a class that inherits from another class (superclass). It can add
its own methods and properties or override parent methods.
5. Define a superclass?
A superclass (base class or parent class) is the class whose properties and behaviors are
inherited by another class.
6. What is an interface?
An interface is a reference type in Java (and other OOP languages) that contains only
abstract methods (until Java 7), and from Java 8 onwards, it can also include default and
static methods.
7. How does method overriding work in inheritance?
Method overriding allows a subclass to provide a specific implementation of a method
that is already defined in the superclass.
java
CopyEdit
class Animal {
     void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
     void sound() { System.out.println("Bark"); }
}
8. What is the role of the super keyword in inheritance?
   ● Access superclass methods and constructors
   ● Call the immediate parent class constructor
java
CopyEdit
super(); // call parent constructor
super.methodName(); // call parent method
9. Can constructors be inherited?
No, constructors are not inherited. However, a subclass can call the superclass’s
constructor using super().
10. How does the final keyword affect inheritance?
   ● A final class cannot be extended.
   ● A final method cannot be overridden.
java
CopyEdit
final class MyClass {} // cannot be inherited
final void method() {} // cannot be overridden
11. Are there any limitations of Inheritance?
   ● Tightly coupled classes
   ● Increased complexity
   ● May break encapsulation
   ● Overuse can lead to fragile base class problem
12. Explain Inheritance with an example:
java
CopyEdit
class Vehicle {
     void start() { System.out.println("Vehicle started"); }
}
class Car extends Vehicle {
     void horn() { System.out.println("Beep!"); }
}
13. Is it possible for a class to inherit the constructor of its base class?
No. Constructors are not inherited but can be invoked using super().
14. What are the limitations of inheritance?
   ● Promotes tight coupling
   ● Can introduce unexpected behavior if not properly managed
   ● Doesn’t support code hiding (every child knows parent details)
15. What is the difference between Inheritance and Polymorphism?
              Feature        Inheritance                     Polymorphism
             Purpos      Enables code            Enables method to behave
             e           reuse                   differently
             Type        "Is-a" relationship     Many forms of a method
             Exampl      Dog extends             Overridden sound() in Dog, Cat
             e           Animal
Shall we proceed to 6. Polymorphism?
You said:
continue
ChatGPT said:
Let’s dive into one of the most powerful features of OOP:
🌀 6. Polymorphism
1. What is Polymorphism?
Polymorphism means "many forms." It allows the same method or function to behave
differently based on the object that is calling it.
There are two main types:
    ● Compile-time polymorphism (Method Overloading)
    ● Runtime polymorphism (Method Overriding)
2. What is the difference between method overloading and method overriding?
                Feature      Method Overloading             Method Overriding
        Occurs In           Same class                Parent-child class
                                                      relationship
        Based On            Number/type of            Implementation of method
                            parameters
        Time of             Compile-time              Runtime
        Resolution
        Purpose             Increase method           Customize inherited
                            flexibility               behavior
3. What is meant by static polymorphism?
Also known as compile-time polymorphism, this occurs when multiple methods have the
same name but different signatures.
java
CopyEdit
class MathUtils {
     int add(int a, int b) { return a + b; }
     double add(double a, double b) { return a + b; }
}
4. What is meant by dynamic polymorphism?
Also called runtime polymorphism, it happens when a method in a subclass overrides a
method in the superclass.
java
CopyEdit
class Animal {
     void sound() { System.out.println("Animal sound"); }
}
class Cat extends Animal {
     void sound() { System.out.println("Meow"); }
}
5. Can we override a static method? Why or why not?
No. Static methods belong to the class, not the instance. Hence, they cannot be
overridden. If you declare the same static method in a subclass, it's method hiding, not
overriding.
6. What is dynamic method dispatch?
Dynamic Method Dispatch is the process of calling an overridden method at runtime,
not compile time, using a reference of the parent class pointing to a child class object.
java
CopyEdit
Animal obj = new Dog();
obj.sound(); // Executes Dog's version
7. What is operator overloading?
Operator overloading allows you to define or alter the meaning of operators for
user-defined types.
   ● Supported in C++
   ● Not supported in Java (except + for string concatenation)
Example in C++:
cpp
CopyEdit
class Complex {
    int real, imag;
    Complex operator + (Complex const &obj) { ... }
};
8. How does C++ support Polymorphism?
C++ supports:
   ● Compile-time polymorphism: via function overloading and operator overloading
   ● Runtime polymorphism: via virtual functions and method overriding
9. What is Compile time Polymorphism and how is it different from Runtime Polymorphism?
             Feature               Compile-Time                  Runtime
                                   Polymorphism               Polymorphism
         Other Name        Static Polymorphism           Dynamic Polymorphism
         Mechanism         Method Overloading            Method Overriding
         Resolution        At Compile Time               At Runtime
         Time
         Flexibility       Less flexible                 More flexible
10. Explain overloading and overriding with examples.
Overloading:
java
CopyEdit
void print(int a) { }
void print(String b) { }
Overriding:
java
CopyEdit
class Parent {
     void show() { System.out.println("Parent"); }
}
class Child extends Parent {
     void show() { System.out.println("Child"); }
}
11. Give a real-world example of polymorphism.
Example:
An interface Shape with a method draw(). Different classes (Circle, Rectangle,
Triangle) implement draw() in their own way.
java
CopyEdit
Shape s1 =      new Circle();
Shape s2 =      new Rectangle();
s1.draw();      // Draws circle
s2.draw();      // Draws rectangle
✅ Shall we move on to the next section: 7. Advanced OOP Concepts?
You said:
continue
ChatGPT said:
Awesome! Now let’s cover the advanced OOP concepts that often come up in technical
interviews:
⚙️ 7. Advanced OOP Concepts
1. What are access specifiers and what is their significance?
Access specifiers define the scope and visibility of class members.
   ● public: Accessible everywhere
   ● private: Accessible only inside the class
   ● protected: Accessible in the same package and subclasses
   ● (default): Accessible within the same package
✅ They help enforce encapsulation and restrict unauthorized access to sensitive data.
2. What is a static method? How is it different from an instance method?
         Feature          Static Method                    Instance Method
        Binding      Belongs to the class         Belongs to an object
        Call Style ClassName.method() object.method()
        Access       Can access only static       Can access both static & instance
                     data                         data
        Example      Math.max(5, 10)              emp.getName()
3. What is a singleton class? How do you implement it?
A singleton class allows only one instance to be created during the program lifecycle.
Java Implementation:
java
CopyEdit
class Singleton {
    private static Singleton instance = null;
    private Singleton() {}
    public static Singleton getInstance() {
       if (instance == null) instance = new Singleton();
       return instance;
    }
}
4. What are design patterns in OOP?
Design patterns are proven solutions to common software design problems. They help
in writing maintainable, scalable code.
Examples:
    ● Singleton
    ● Factory
    ● Observer
    ● Strategy
    ● Decorator
5. What is dependency injection?
It’s a design pattern where an object’s dependencies are provided from the outside,
rather than the object creating them itself.
Benefits:
    ● Reduces coupling
    ● Improves testability
6. What is the difference between composition and aggregation?
       Feature                  Composition                         Aggregation
     Dependen       Strong (child cannot exist           Weak (child can exist
     cy             alone)                               independently)
     Life Cycle     Tied to parent                       Independent
     Example        House → Rooms                        Team → Players
7. How do mixins work in OOP?
A mixin is a class that provides methods to other classes without being a parent class.
It’s a way to share functionality horizontally (commonly used in Python, Ruby, etc.).
8. What is composition?
Composition is an OOP principle where a class has an object of another class, rather
than inheriting from it.
java
CopyEdit
class Engine { ... }
class Car {
     Engine engine = new Engine(); // Car "has-a" Engine
}
9. What is the difference between Composition and Inheritance?
                                Feature       Inheritance    Compositio
                                                                n
                           Relationship       "is-a"         "has-a"
                           Type
                           Flexibility        Less           More
                                              flexible       flexible
                           Coupling           Tighter        Looser
10. What is constructor chaining?
Constructor chaining is when one constructor calls another constructor in the same
class using this() or the superclass constructor using super().
java
CopyEdit
class Car {
     Car() { this(10); }
     Car(int speed) { System.out.println(speed); }
}
11. What is Coupling in OOP, and why is it helpful?
Coupling refers to the degree of dependency between classes.
   ● Loose coupling is preferred: It makes code more maintainable, testable, and
      modular.
   ● Tight coupling makes the system rigid and hard to modify.
12. Name the operators that cannot be overloaded.
In C++, the following cannot be overloaded:
   ● :: (scope resolution)
   ● . (member access)
   ● .* (member pointer access)
   ● sizeof
   ● typeid
13. What is the difference between new and override?
In C#/C++:
    ● new hides the parent method (method hiding)
    ● override replaces the parent method (method overriding)
14. What is Cohesion in OOP?
Cohesion refers to how closely related the responsibilities of a class are.
    ● High cohesion = better organization and readability
    ● Low cohesion = class is doing too many unrelated things
15. What is the difference between a base class and a superclass?
Both terms refer to the parent class in inheritance. They are used interchangeably:
    ● Base class is more common in C++
    ● Superclass is often used in Java
✅ Let me know if you'd like to continue with 8. Exception Handling in OOP.
You said:
continue
ChatGPT said:
Let’s continue with:
⚠️ 8. Exception Handling in OOP
1. What is an exception?
An exception is an event that occurs during program execution that disrupts the normal
flow of instructions. It typically occurs due to invalid input, file issues, or resource errors.
2. What is meant by exception handling?
Exception handling is a mechanism to handle runtime errors so that the program’s flow
can be maintained smoothly without abrupt termination.
Languages like Java, Python, and C++ provide constructs like:
java
CopyEdit
try {
     // code that may throw an exception
} catch (Exception e) {
     // handling code
} finally {
     // cleanup code
}
3. How do you handle exceptions in OOP?
   ● Java/C++/C#: Using try, catch, finally, and throw/throws keywords.
   ● Python: Using try, except, finally, and raise.
Example (Java):
java
CopyEdit
try {
     int result = 10 / 0;
} catch (ArithmeticException e) {
     System.out.println("Division by zero!");
} finally {
     System.out.println("Cleanup done.");
}
4. What is the difference between checked and unchecked exceptions (Java)?
       Feature         Checked Exception                   Unchecked Exception
Inheritance          Inherits from               Inherits from RuntimeException
                     Exception
Compiler             Checked at                  Checked at runtime
Handling             compile-time
Example              IOException,                NullPointerException,
                     SQLException                ArithmeticException
5. Can a constructor throw an exception?
Yes. In Java, a constructor can throw both checked and unchecked exceptions.
java
CopyEdit
public class MyClass {
     public MyClass() throws IOException {
        // some file operation
     }
}
6. What is the finally block used for?
The finally block executes regardless of whether an exception is thrown or caught.
It is typically used for cleanup, like closing files or releasing resources.
7. What are the differences between error and exception?
 Feature                   Error                                Exception
Type        Irrecoverable (e.g.,                  Recoverable (e.g.,
            OutOfMemoryError)                     FileNotFoundException)
Handlin     Not meant to be caught                Can be handled using try-catch
g
Origin      Occurs due to environment             Occurs due to code or logic
✅ Would you like to continue with 9. OOP in Practice?
You said:
continue
ChatGPT said:
Let’s move ahead to:
🧩 9. OOP in Practice
1. How does OOP compare to procedural programming?
      Feature            Procedural Programming        Object-Oriented Programming
 Structure           Based on functions             Based on objects and classes
 Code Reusability    Limited                        High (via inheritance and
                                                    polymorphism)
 Modularity          Hard to manage in large        Promotes modularity
                     projects
 Example             C                              Java, Python, C++
 Language
2. What are the advantages of using OOP in software development?
    ● Reusability through inheritance
    ● Modularity via classes and objects
    ● Scalability using abstraction and polymorphism
    ● Maintainability with encapsulated, well-structured code
    ● Real-world modeling through object representation
3. Can you give an example of an OOP-based real-world application?
Banking System:
   ● Account is a superclass.
   ● SavingsAccount and CurrentAccount inherit from it.
   ● Methods like deposit(), withdraw() are overridden as needed.
   ● Each Customer is represented as an object with personal and account info.
4. What are some common pitfalls when using OOP?
   ● Overuse of inheritance instead of composition
   ● Tight coupling between classes
   ● Poor encapsulation
   ● Using objects where simple data structures are sufficient
   ● Ignoring design principles like SOLID
5. How does OOP support code scalability and maintainability?
   ● Through modularity, code can be divided into smaller, manageable units.
   ● Polymorphism allows adding new behaviors without changing existing code.
   ● Inheritance avoids duplication.
   ● Encapsulation protects internal states and simplifies changes.
6. Can we run a Java application without implementing the OOPs concept?
Technically, yes — simple Java applications (like HelloWorld) can run using just static
methods (main). But Java is designed for OOP, and real-world apps typically require
objects and classes.
7. Identify which OOPs concept should be used in the following scenario:
      “A group of 5 friends, one boy never gives any contribution when the group
      goes for the outing. Suddenly a beautiful girl joins the same group. The boy
      who never contributes is now spending a lot of money for the group.”
✅ Answer: Polymorphism
Explanation: The boy (object) behaves differently in different contexts (before and after
the girl joins). This is runtime polymorphism — the same object shows different behavior
based on runtime situation.
✅ Ready to finish with the final section: 10. Miscellaneous OOP Questions?
You said:
yes
ChatGPT said:
Perfect! Let's wrap up with:
🧠 10. Miscellaneous OOP Questions
1. What is the difference between shallow copy and deep copy?
                Copy Type                        Description
                Shallow     Copies reference to original objects (nested objects
                            shared)
                Deep        Creates copies of everything, including nested
                            objects
2. How does garbage collection work in OOP languages?
Garbage collection automatically reclaims memory occupied by objects that are no
longer reachable or referenced.
    ● In Java: Done by the JVM
   ● In Python: Handled by the interpreter
   ● C++: Manual memory management (via delete)
3. What is meant by Garbage Collection in OOPs world?
Garbage Collection is a process where unreferenced objects are identified and removed
from memory, helping prevent memory leaks and improve performance.
4. What is method chaining?
Method chaining is a technique where multiple methods are called in a single
statement, using the return value of the previous method (usually this).
java
CopyEdit
builder.setName("Abhi").setAge(21).setEmail("a@gmail.com");
5. What is the difference between mutable and immutable objects?
         Feature              Mutable Object               Immutable Object
       Changeabl Can be changed after                Cannot be changed after
       e         creation                            creation
       Examples      ArrayList,                      String, Integer (Java)
                     StringBuilder (Java)
6. What is reflection in OOP?
Reflection is the ability of a program to inspect and modify its own structure (classes,
methods, fields) at runtime.
In Java, done using the java.lang.reflect package:
java
CopyEdit
Class<?> clazz = Class.forName("MyClass");
Method m = clazz.getMethod("myMethod");
7. Are class and structure the same? If not, what's the difference between a class and a
structure?
              Feature              Class                       Structure
          Default           private (Java/C++) public (C++)
          Access
          Inheritance       Supported               Limited (not in C, partial in C++)
          Memory            Reference Type          Value Type (C/C++)
                            (Java)
8. What are the various types of constructors in C++?
   ● Default constructor
   ● Parameterized constructor
   ● Copy constructor
   ● Destructor (not a constructor but related)
9. What is a copy constructor?
A copy constructor creates a new object by copying data from an existing object.
cpp
CopyEdit
class MyClass {
    MyClass(const MyClass &obj) { ... }
};
10. What is a destructor?
A destructor is a special function that gets called automatically when an object goes
out of scope. It cleans up resources.
In C++:
cpp
CopyEdit
~MyClass() { }
11. What are the manipulators in OOP, and how do they work?
In C++, manipulators are used to format output, e.g. endl, setw, setprecision.
cpp
CopyEdit
cout << setw(10) << "Hello";
12. What are the rules for creating a constructor?
   ● Must have the same name as the class
   ● Cannot have a return type
   ● Can be overloaded
   ● Can be private, protected, or public
13. What are the differences between the constructor and the method in Java?
                  Feature           Constructor               Method
                Purpose        Initialize object      Perform operations
                Name           Same as class name     Any name
                Return         No return type         Must have return
                Type                                  type
                Call Time      Called at object       Called explicitly
                               creation
14. What are the types of variables in OOP?
    ● Instance variables – tied to object
    ● Class (static) variables – shared among all objects
    ● Local variables – defined within methods
    ● Parameter variables – method arguments
15. Is it possible to overload a constructor?
Yes. You can create multiple constructors with different parameters (constructor
overloading).
java
CopyEdit
class Student {
     Student() {}
     Student(String name) {}
}
16. Can we overload the main() method in Java? Also, give an example.
Yes, main() can be overloaded but only the standard one with public static void
main(String[] args) is used by JVM.
java
CopyEdit
public class Test {
     public static void main(String[] args) {
        System.out.println("Hello main");
        main(10);
     }
    public static void main(int a) {
       System.out.println("Overloaded main: " + a);
    }
}
✅   That’s it! You now have a complete set of OOP interview questions and answers —
all explained clearly so you can speak them confidently in interviews.