Top 27 OOPs Concept Interview Questions Answers

This post walks you through Top 19 OOPs Concept Interview Questions Answers in Java. OOPS stands for Object-oriented programming, is a programming pattern based on the idea of “objects”, which may comprise data, in the form of fields, frequently known as attributes; and code, in the form of procedures, often identified as methods. Having expertise in OOPS will place you an ideal career.

Top 19 OOPs Concept Interview Questions Answers

1.What are the main principles of Object Oriented Programming?
Main principles of Object Oriented Programming (OOPS) are:

  • 1. Abstraction
  • 2. Encapsulation
  • 3. Inheritance
  • 4. Polymorphism

2.What is the difference between Object Oriented Programming language and Object Based Programming language?
Object Oriented Programming languages like Java and C++ follow concepts of OOPS like- Encapsulation, Abstraction, Polymorphism and Inheritance etc.
Object Based Programming languages follow some features of OOPS but they do not provide support for Polymorphism and Inheritance. Egg. JavaScript, VBScript etc.
Object Based Programming languages provide support for Objects and you can build objects from constructor. They languages also support Encapsulation. These are also known as Prototype-oriented languages.

3.In Java what is the default value of an object reference defined as an instance variable in an Object?
All the instance variable object references in Java are null.

4.Why do we need constructor in Java?
Java is an object-oriented language, in which we create and use objects. A constructor is a piece of code similar to a method. It is used to create an object and set the initial state of the object.
A constructor is a special function that has same name as class name.
Without a constructor, there is no other way to create an object.
By default, Java provides a default constructor for every object. If we overload a constructor then we have to implement default constructor.

5.Why do we need default constructor in Java classes?
Default constructor is the no-argument constructor that is automatically generated by Java if no other constructor is defined.
Java specification says that it will provide a default constructor if there is no overloaded constructor in a class. But it does not say anything about the scenario in which we write an overloaded constructor in a class.
We need at least one constructor to create an object, that’s why Java provides a default constructor.
When we have overloaded constructor, then Java assumes that we want some custom treatment in our code. Due to which it does not provide default constructor. But it needs default constructor as per the specification. So it gives error.

6.What is the value returned by Constructor in Java?
When we call a constructor in Java, it returns the object created by it. That is how we create new objects in Java.

7.Can we inherit a Constructor?
No, Java does not support inheritance of constructor.

8. Why constructors cannot be final, static, or abstract in Java?
If we set a method as final it means we do not want any class to override it. But the constructor (as per Java Language Specification) cannot be overridden. So there is no use of marking it final.
If we set a method as abstract it means that it has no body and it should be implemented in a child class. But the constructor is called implicitly when the new keyword is used. Therefore it needs a body.
If we set a method as static it means that it belongs to the class, but not a particular object. The constructor is always called to initialize an object. Therefore, there is no use of marking constructor static.

9.What is the purpose of ‘this’ keyword in java?
In Java, ‘this’ keyword refers to current instance of the object.
It is useful for differentiating between instance variables and local variables.
It can be used to call constructors. Or it can be used to refer to the instance.
In case of method overriding, this is used for falling the method of current class.

10. Explain the concept of Inheritance?
Inheritance is an important concept in Object Oriented Programming. Some objects share certain characteristics and
behavior. By using Inheritance, we can put the common behavior and characteristics in a base class which also known as super class. And then all the objects with common behavior inherit from this base class.
It is also represented by IS-A relationship.
Inheritance promotes, code reuse, method overriding and polymorphism.

11. Which class in Java is superclass of every other class?
Java is an object oriented programming language. In Java, Object class is the superclass of every other class.

12. Why Java does not support multiple inheritance?
Multiple Inheritance means that a class can inherit behavior from two or more parent classes.
The issue with Multiple Inheritance is that both the parent classes may have different implementation for the same method. So they have different ways of doing the same thing. Now which implementation should the child class choose?
This leads to ambiguity in Multiple Inheritance. This is the main reason for Java not supporting Multiple Inheritance in implementation.
Lets say you have a class TV and another class AtomBomb. Both have method switchOn() but only TV has switchOff() method. If your class inherits from both these classes then you have an issue that you can switchOn() both parents, but switchOff will only switchOff() TV.
But you can implement multiple interfaces in Java.

13. In OOPS, what is meant by composition?
Composition is also known as “has-a” relationship. In composition, “has-a” relation relates two classes. E.g. Class Car has a steering wheel.
If a class holds the instance of another class, then it is called composition.

14. How aggregation and composition are different concepts?
In OOPS, Aggregation and Composition are the types of association relations. A composition is a strong relationship. If the composite object is destroyed, then all its parts are destroyed. E.g. A Car has a Steering Wheel. If Car object is destroyed, then there is no meaning of Steering Wheel.
In Aggregation, the relationship is weaker than Composition.
E.g. A Library has students. If a Library is destroyed, Students still exist. So Library and Student are related by Aggregation. A Library has Books. If Library is destroyed, the Books are also destroyed.
Books of a Library cannot exist without the Library. So Book and Library are related by Composition.

15. Why there are no pointers in Java?
In Java there are references instead of pointers. These references point to objects in memory. But there is no direct access to these memory locations. JVM is free to move the objects within VM memory.
The absence of pointers helps Java in managing memory and garbage collection effectively. Also it provides developers with convenience of not getting worried about memory allocation and deallocation.

16. If there are no pointers in Java, then why do we get NullPointerException?
In Java, the pointer equivalent is Object reference. When we use a. it points to object reference. So JVM uses pointers but programmers only see object references.
In case an object reference points to null object, and we try to access a method or member variable on it, then we get NullPointerException.

17. What is the purpose of ‘super’ keyword in java?
‘super’ keyword is used in the methods or constructor of a child class. It refers to immediate parent class of an object.
By using ‘super’ we can call a method of parent class from the method of a child class.
We can also call the constructor of a parent class from the constructor of a child class by using ‘super’ keyword.

18. Is it possible to use this() and super() both in same constructor?
No, Java does not allow using both super() and this() in same constructor. As per Java specification, super() or this() must be the first statement in a constructor.

19.What is the meaning of object cloning in Java?
Object.clone() method is used for creating an exact copy of the object in Java. It acts like a copy constructor. It creates and returns a copy of the object, with the same class and with all the fields having same values as of the original object.
One disadvantage of cloning is that the return type is an Object. It has to be explicitly cast to actual type.

20. In Java, why do we use static variable?
Whenever we want to have a common property for all objects of a class, we use a class level variable i.e. a static variable.
This variable is loaded in memory only once at the time of class loading. So it saves memory, since it is not defined per object in Java.

21. Why it is not a good practice to create static variables in Java?
Static variables are common to all the objects of a class. If a new object is created, there is no need to test the value of static variable. Any code that uses static variable can be in any state. It can be within a new object or at a class level. So the scope of static variable is open ended in a Java class.
If we want tighter control on scope, then variables should be created at the object creation level.
Also defining static variables is not a good practice because they go against the principles of Object Oriented Programming.

22. What is the purpose of static method in Java?
Java provides the feature of static method to create behavior at the class level. The static method is common to all the objects of a class. We do not need to create any object of a class to call a static method. So it provides convenience of not creating an object for calling it.
Also a static method can access and modify static data members. This also helps in keeping the behavior as well as state at the class level.

23. Why do we mark main method as static in Java?
The main method in Java is marked as static, so that JVM can call it to start the program. If main method is not static, then which constructor will be called by Java process?
As such it is a known as convention to mark main method static in Java. But if we remove the static, then there will be ambiguity. Java process may not know which method of a class to call to start the program.
So this convention helps in Java process to identify the starting code for a program in class that is passed as an argument to java process.

24. In what scenario do we use a static block?
At times, there is a class that has static member variables. These variables need some complicated initialization. At this time static block helps as a tool to initialize complex static member variable initialization.
The static block is executed even before the execution of main.
Sometimes, we can also replace static block with a static method of class.

25. Is it possible to execute a program without defining a main() method?
No, with Java 7 onwards, you need a main() method to execute a program. In earlier versions of Java, there was a workaround available to use static blocks for execution. But now this gap has been closed.

26. What happens when static modifier is not mentioned in the signature of main method?
As per Java specification, main method has to be marked as static. It needs only one argument that is an array of String.
A program can compile with a non-static method. But on execution it will give NoSuchMethodError.

27. What is the difference between static method and instance method in Java?
Often, there is a need to define a behavior for a class that is not dependent on member variables of an object. Such behavior is captured in a static method. If there is a behavior dependent upon the member variables of an object, then we do not mark it static, it remains as instance method.
To call as static method, we do not need to create an object. We just call it with class name. But to call an instance method, we need to create/get an object first.
Instance member variables cannot be accessed by a static method. But an instance method can call both instance variables and static variables.

That’s all about Top 27 OOPs Concept Interview Questions Answers.

Leave a Comment

Please share it if you found this useful
Hide Buttons