OOP Interview QA
OOP Interview QA
1. Encapsulation
2. Inheritance
3. Polymorphism
4. Abstraction
A: A class is a blueprint for creating objects, defining their attributes and behaviors. An object is an
instance of a class.
A: Encapsulation is the bundling of data and methods that operate on the data within one unit, e.g.,
A: Inheritance allows a class to inherit fields and methods from another class. Types include:
1. Single
2. Multilevel
3. Hierarchical
5. Hybrid
A: Polymorphism allows objects to be treated as instances of their parent class. Overloading means
same method name with different parameters. Overriding means redefining a method in a subclass.
A: Abstraction hides implementation details and shows only functionality. In Java, it's implemented
using abstract classes and interfaces. In Python, abstract base classes (ABC) are used.
A: An interface defines a contract with no implementation. An abstract class can have both abstract
and concrete methods. Java interfaces can't have instance fields, unlike abstract classes.
A: Overloading is having multiple methods with the same name but different parameters. Overriding
A: Multiple inheritance is when a class inherits from more than one class. Java does not support it
A: The diamond problem occurs in multiple inheritance when two parent classes have a method with
the same name. Java avoids it by not supporting multiple inheritance with classes.
Composition: A strong 'has-a' relationship where child cannot exist without parent.
Q: What are access modifiers in OOP? (private, protected, public)
A: 'super' refers to the parent class and is used to access parent methods/constructors.
A: No, private methods are not accessible in subclasses. Static methods belong to the class, not
A: Shallow copy copies references to objects. Deep copy copies the objects themselves, creating
independent duplicates.
A: A virtual function is a member function that can be overridden in a derived class and is resolved
at runtime.
A: An abstract class is a class that cannot be instantiated and may contain abstract methods. Yes, it
A: Design patterns are typical solutions to common problems in software design. Examples:
S - Single Responsibility
O - Open/Closed
L - Liskov Substitution
I - Interface Segregation
D - Dependency Inversion
A: Dependency Injection is a technique where one object supplies the dependencies of another
A: LoD is a principle that states a method should only interact with its direct friends (limited
A: A singleton class allows only one instance. It's implemented using a private constructor and a
A: An interface defines methods a class must implement (Java). A protocol (Python/Swift) defines a
Q: How does garbage collection work in OOP-based languages like Java or Python?
A: Garbage collection automatically frees memory by deleting unreachable objects. Java uses a
A: Friend functions/classes can access private/protected members of another class. Useful for
operator overloading or utility functions.
supertypes as arguments.