OOPs Interview Questions and Answers
Experienced / Expert level questions & answers
Ques 1. What is the difference between abstraction and encapsulation?
Abstraction involves hiding the unnecessary details while encapsulation involves bundling the data and methods that operate on the data into a single unit.
Ques 2. Explain the concept of abstract classes.
Abstract classes are classes that cannot be instantiated and may contain abstract methods. Subclasses must implement these abstract methods.
Example:
abstract class Shape {
abstract calculateArea();
}
Ques 3. What is the difference between composition and inheritance?
Composition involves combining objects to create more complex ones, while inheritance involves creating a new class by inheriting properties and behaviors from an existing class.
Ques 4. Explain the concept of multiple inheritance.
Multiple inheritance allows a class to inherit properties and behaviors from more than one superclass. Some languages support it directly, while others provide alternatives like interfaces.
Ques 5. What is a friend class in C++?
In C++, a friend class is a class that is not a part of the class hierarchy but is granted access to the private and protected members of another class.
Ques 6. What is the SOLID principle in OOP?
SOLID is an acronym for five design principles in OOP: Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion. These principles aim to make software designs more understandable, flexible, and maintainable.
Ques 7. What is the diamond problem in the context of multiple inheritance?
The diamond problem occurs when a class inherits from two classes that have a common ancestor. It can lead to ambiguity in the inheritance hierarchy, especially if the common ancestor has members.
Ques 8. Explain the term 'covariant return type' in OOP.
Covariant return type allows a method in a subclass to return a more derived type than the method in the base class. It is a feature supported by some programming languages, including C++ and Java.
Most helpful rated by users: