[go: up one dir, main page]

0% found this document useful (0 votes)
4 views5 pages

OOP Interview QA

The document provides a comprehensive overview of Object-Oriented Programming (OOP) concepts, including the four pillars: encapsulation, inheritance, polymorphism, and abstraction. It covers key differences between classes and objects, various types of inheritance, and important principles like SOLID and the Law of Demeter. Additionally, it explains design patterns, access modifiers, and memory management in OOP languages like Java and Python.

Uploaded by

prayasumate1239
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

OOP Interview QA

The document provides a comprehensive overview of Object-Oriented Programming (OOP) concepts, including the four pillars: encapsulation, inheritance, polymorphism, and abstraction. It covers key differences between classes and objects, various types of inheritance, and important principles like SOLID and the Law of Demeter. Additionally, it explains design patterns, access modifiers, and memory management in OOP languages like Java and Python.

Uploaded by

prayasumate1239
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

OOP Interview Questions and Answers

Q: What are the four pillars of OOP?

A: The four pillars of Object-Oriented Programming are:

1. Encapsulation

2. Inheritance

3. Polymorphism

4. Abstraction

Q: Explain the difference between class and object.

A: A class is a blueprint for creating objects, defining their attributes and behaviors. An object is an

instance of a class.

Q: What is encapsulation? How does it improve security?

A: Encapsulation is the bundling of data and methods that operate on the data within one unit, e.g.,

a class. It improves security by restricting direct access to some of an object's components.

Q: Define inheritance and its types.

A: Inheritance allows a class to inherit fields and methods from another class. Types include:

1. Single

2. Multilevel

3. Hierarchical

4. Multiple (via interfaces in Java)

5. Hybrid

Q: What is polymorphism? Explain method overloading and overriding.

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.

Q: What is abstraction? How is it implemented in Java or Python?

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.

Q: How is an interface different from an abstract class?

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.

Q: What is the difference between static and dynamic polymorphism?

A: Static polymorphism (compile-time) is achieved using method overloading. Dynamic

polymorphism (runtime) is achieved using method overriding.

Q: What is the difference between method overloading and method overriding?

A: Overloading is having multiple methods with the same name but different parameters. Overriding

is redefining a superclass method in a subclass with the same signature.

Q: What is the difference between composition and inheritance?

A: Inheritance represents an 'is-a' relationship, while composition represents a 'has-a' relationship.

Composition is favored for better flexibility and encapsulation.

Q: What is multiple inheritance? Does Java support it?

A: Multiple inheritance is when a class inherits from more than one class. Java does not support it

directly due to the diamond problem but supports it via interfaces.

Q: How does Java achieve abstraction?

A: Java achieves abstraction using abstract classes and interfaces.

Q: What is the diamond problem in OOP, and how is it resolved?

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.

Q: Explain the difference between association, aggregation, and composition.

A: Association: General relationship between classes.

Aggregation: A weak 'has-a' relationship.

Composition: A strong 'has-a' relationship where child cannot exist without parent.
Q: What are access modifiers in OOP? (private, protected, public)

A: Private: Accessible within the class only.

Protected: Accessible in the same package and subclasses.

Public: Accessible from any other class.

Q: What is the use of the 'super' and 'this' keyword?

A: 'super' refers to the parent class and is used to access parent methods/constructors.

'this' refers to the current instance of the class.

Q: Can we override a private or static method? Why or why not?

A: No, private methods are not accessible in subclasses. Static methods belong to the class, not

instances, so they can't be overridden.

Q: What is the difference between deep copy and shallow copy?

A: Shallow copy copies references to objects. Deep copy copies the objects themselves, creating

independent duplicates.

Q: What is a virtual function in C++?

A: A virtual function is a member function that can be overridden in a derived class and is resolved

at runtime.

Q: What is an abstract class, and can it have a constructor?

A: An abstract class is a class that cannot be instantiated and may contain abstract methods. Yes, it

can have a constructor.

Q: What are design patterns? Name a few commonly used ones.

A: Design patterns are typical solutions to common problems in software design. Examples:

Singleton, Factory, Observer, Strategy.

Q: What is SOLID in OOP, and why is it important?

A: SOLID is a set of principles for object-oriented design:

S - Single Responsibility
O - Open/Closed

L - Liskov Substitution

I - Interface Segregation

D - Dependency Inversion

Q: What is dependency injection, and how does it relate to OOP?

A: Dependency Injection is a technique where one object supplies the dependencies of another

object. It promotes loose coupling.

Q: Explain the Law of Demeter (LoD).

A: LoD is a principle that states a method should only interact with its direct friends (limited

knowledge). It promotes low coupling.

Q: What is a singleton class? How do you implement it?

A: A singleton class allows only one instance. It's implemented using a private constructor and a

static method to return the instance.

Q: What is the difference between an interface and a protocol in OOP?

A: An interface defines methods a class must implement (Java). A protocol (Python/Swift) defines a

contract for behavior, optionally with default implementations.

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

generational GC. Python uses reference counting and cycle detection.

Q: What is the difference between final, finally, and finalize in Java?

A: final: Used to declare constants or prevent inheritance/overriding.

finally: Block used after try-catch for cleanup.

finalize: Method called by GC before object destruction.

Q: What are friend functions and friend classes in C++?

A: Friend functions/classes can access private/protected members of another class. Useful for
operator overloading or utility functions.

Q: What is the difference between covariance and contravariance in OOP?

A: Covariance allows a method to return a subtype. Contravariance allows a method to accept

supertypes as arguments.

You might also like