Java Interview Questions – Core Java & OOPS

1. [Amazon] What is the difference between an abstract class and an interface?
Answer:

  • An abstract class can have constructors, instance variables, and concrete methods.
  • An interface only contains abstract methods (before Java 8) and no instance variables.
  • A class can implement multiple interfaces but can extend only one abstract class.

2. [Google] What is object slicing in Java?

Answer: Object slicing occurs when an object of a subclass is assigned to a superclass reference, losing subclass-specific attributes.


3. [Microsoft] What is the use of super() in Java?

Answer: super() is used to call the constructor of a parent class to initialize inherited properties.


4. [Netflix] Can a constructor be private in Java?

Answer: Yes, a private constructor is used in Singleton patterns to prevent object creation from outside the class.


5. [Facebook] Can we declare a constructor as static in Java?

Answer: No, constructors cannot be static because they belong to an instance, while static belongs to the class.


6. [Oracle] What happens if you remove the main method from a Java application?

Answer: The JVM throws a NoSuchMethodError at runtime.


7. [Uber] Can an interface have a constructor?

Answer: No, an interface cannot have a constructor because it is not meant to be instantiated.


8. [Deloitte] What is method hiding in Java?

Answer: If a subclass defines a static method with the same signature as a static method in the superclass, it hides the superclass method instead of overriding it.


9. [JP Morgan] What is the difference between instanceof and getClass() in Java?
Answer:

  • instanceof checks whether an object is an instance of a class or its subclass.
  • getClass() checks for the exact class type of an object.

10. [Goldman Sachs] How does a finally block behave when there is a return statement inside a try block?

Answer: The finally block will always execute even if there is a return statement inside the try block.