Top OOP (Object Oriented Programming) Interview Questions and Answers
Danish Saleem
Posted on December 1, 2021
As a good software engineer you should have a good OOP (Object Oriented Programming) concept. If you have an interview for the position of Software Engineer, you should expect the OOP interview questions that I am going to share in the post below. Don't forget to bookmark this page as this might help you in future as well 🙂
OOP Interview Questions and Answers
1. What is OOP?
OOP stands for Object Oriented Programming. It is a programming paradigm in which everything is represented as an object and the program is designed using classed and objects.
2. What is an Object?
An Object is a real-world entity that has state and behavior like bulb, chair, table etc.
3. What is a Class?
A Class is called the blueprint of an object.
4. What are the advantages of OOP?
The advantage of OOP includes re-usability and easy maintenance.
5. Difference between object-based and object-oriented programming?
Object-based programming is almost the same methodology as the object-oriented programming but it does not support Inheritance. JavaScript is one of the examples of Object-based programming.
6. What is a Constructor?
It is a special type of method which invoked at the time of object creation.
7. What is the difference between Constructor and Method?
A constructor must not have any return type and its name should be same as the class name. While a method must have a return type while its name may or may not be same as the class name.
8. Does Constructor return any value?
The constructor returns the class instance.
9. What are the pillars of OOP?
There are four pillars of OOP:
- Inheritance
- Polymorphism
- Abstraction
- Encapsulation
10. What is Inheritance?
It is a concept in which the child class inherits all the attributes and behaviors of its parent class.
11. What is Multiple Inheritance and Multilevel Inheritance?
In multiple inheritance, a class inherits attributes and behaviors from multiple parent classes. In multilevel inheritance, a class extends a class, which extends another class.
12. What is Aggregation?
When a class contains a reference to another class, this concept is called Aggregation.
13. When should opt for Inheritance and Aggregation?
When there is an IS-A relationship between the two entities, we will opt Inheritance.
For example, Cat and Animal (Cat is a(n)) Animal.
While, when there is a HAS-A relationship between the two entities, we will opt Aggregation.
For example, Class and Student (Class has a student).
14. What is the difference between Aggregation and Composition?
The composition is a strong association between two entities. For example, Car and Engine. If Car is destroyed, Engine is of no use. As compared to this, entities are independent of each other in Aggregation. For example, Class and Student. If Class is destroyed, Student can still exist.
15. What is Polymorphism?
In OOP, the ability of the objects to respond in different manner to identical messages is called Polymorphism. It is achieved by method overloading and method overriding.
16. What is method overloading?
If two or more than two methods in the same class have the same name but different arguments, this concept is called method overloading. It is also called static binding or compile time polymorphism.
17. What is method overriding?
If a method defined in a parent class is redefined I the child class, this concept is call method overriding. It is also called dynamic binding or runtime polymorphism.
18. What is Encapsulation?
Hiding the state or behavior of an object is called Encapsulation. It is achieved using access modifiers.
19. What are access modifiers?
Access modifiers are the keywords that define the scope of the attribute or the function.
20. What is Abstraction?
Hiding the certain complex details and showing the essential features of the object is called Abstraction.
21. What is an abstract method?
A method that is declared but has no definition.
22. What is a virtual method?
It is a member method of a class which is expected to be overridden in it derived or child class.
23. What is the difference between abstract and virtual method?
A virtual method can have a definition while abstract method can't have any definition.
24. What is the use of the "static" keyword?
If a variable or method is declared static, it gets associated with the class instead of the object. Static variables and methods can be called without the creation of the instance of the class they are in.
25. What is the use of the "Super" keyword?
It is used to invoke the constructor of the parent class in the child class. It is also used to invoke the method defined in the parent class which is overridden in the child class.
26. What is the difference between abstraction and encapsulation?
Encapsulation is hiding the internal details of the object while abstraction is showing the essential features and hiding the internal complexity.
27. What is an Interface?
It is a contract that enforces a class to implement all the abstract methods declared un that interface.
28. What is the difference between abstract class and interface?
In the abstract class, partial abstraction is achieved (0-100%) while through the interface, 100% abstraction is achieved. All the methods in the interface are abstract while abstract class contains both abstraction and non-abstract method.
29. Can Overloaded methods have different return types?
Yes, the compiler is smart enough to distinguished the methods with difference return types but their arguments can' be same.
This will give a compile time error:
public void calculates (int num) {
...
}
public int calculate (int num) {
...
}
However, this will be successfully compiled:
public void calculates (int num) {
...
}
public int calculate (int num, int numExtra) {
...
}
30. Assume there is a parent class "Animal" and a child class "Cat". Can the below code be executed?
I) Animal a = new Cat ();
This code can be executed as the parent class object can have a reference to the child class object.
ii) Cat c = new Animal ();
This code will throw an error as the child class object cannot have a reference to the parent class object.
31. What isa concrete class?
It is a class that does not define an abstract method of its own.
I hope the above OOP interview questions will prove to be useful for you. If you like it share it with your friends to whom you feel it will be useful.
Best of Luck!
Let's connect 💜
You can follow me on Twitter, Instagram & GitHub
If you like this post. Kindly support me by Buying Me a Coffee
Posted on December 1, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.