Top Core Java Interview Questions


1. What is the difference between transient and volatile variable?

volatile variable means a way to indicate that variable needs to be always checked for the latest value instead of being fetched from a cached value. This is used when there are multiple threads and one or more threads needs to get the volatile variable.
transient variable is a variable whose value wont be serialized when the object containing it is serialized. Only memeber variables can be transient. transient is not applicable for classes, interfaces, methods and static variables.

2. What is serialization and deserialization?

Serialization is the process of converting an object into a string which can be stored in a permanent storage location accessible via a file system or database.
De-serialization is the process of converting a serialized object into its regular form.

3. What is the difference between static variable and non-static variable?

Static member variable is allocated memory only once per class. Static members can be accessed directly by using class name or by using instance variable. However, there is only one value stored per class and all instances would refer to the same value
Instance member variable is allocated memory whenever a new object is created. Non-Static members (instance members) can be accessed by using object reference

4. What is the difference between static method and non-static method?

Static method can not directly access non-static variables of the class. However an instance can be created inside the static method and variables of that instance can be accessed or modified.
Non-Static method (instance method) can be used to change static or non-static (instance) variables of the class

5. What is the difference between static nested class and non-static nested class?

Static nested class (just like static method) can not access the member variables of the enclosing class.
Non-Static nested class (inner class) is also called inner class. Inner class has access to the member variables and methods of the enclosing class (outer class)

6. What is anonymous inner class?

Anonymous inner class is a type of inner class which does not have a class name
     new Example(new Sample(){ System.out.println("Hello"); });

7. What is the difference between method overloading and method overriding?

Method overloading is writing multiple methods within the same class with method differing in its signature.

Method signature is identified by the method with

  • same name and
  • same number of formal arguments,
  • same data type of method arguments and
  • same order of data type of method arguments and
  • not differing in its return type and
  • not differing in the throws class

Method over-riding is writing methods in the subclass with the same method signature as parent class.

8. What is encapsulation?

Encapsulation is the process of hiding properties and methods inside a class so that they cannot be directly accessed or modified to avoid unexpected behaviour. Instead, access to the properties and methods are made possible via other methods which provides control over access to the data.

9. What is polymorphism?

Polymorphism is the ability to be available in multiple forms.

10. What are the types of polymorphism?

Static polymorphism and dynamic polymorphism

11. What is static polymorphism or static binding ?

Static polymorphism or static binding is compile-time polymorphism. Method overloading is an example of static binding. The method to be invoked is bound to an instance variable during compile time itself. One look at the line of method invocation itself in the source code will be able to show the method that is going to be invoked

12. What is dynamic polymorphism or Dynamic binding?

Dynamic polymorphism or Dynamic binding is runtime polymorphism. Method overriding is an example of Dynamic binding

13. What is dependency injection?

Dependency injection is the design principle by dependencies are not created by the line of code, instead created by the container and injected into the parent object.

14. What is Inversion of control?

If the decision to create objects flows from the code you write to the container or server runtime, it is regular flow of control. If the control flows from server or container to the creation of object, it is inversion of control.

15. What is inheritance?

Inheritance is the process of inheriting the properties and behaviour from another class.

public class Vehicle{
		int numberOfWheels;
		void drive(){
		}
	}
	public class Car extends Vehicle{
		double steeringWheelRadius;
		void playMusic(){
		}
	}
	

In this example, Car inherits the properties of Vehicle by extending it.

16. What is multiple inheritance?

Multiple inheritance is a class inheriting from multiple classes.

17. Does java support multiple inheritance?

Java does not support Multiple inheritance of classes

18. Why is Java not supporting multiple inheritance?

Java does not support multiple inheritance as it may give rise to the diamond problem.

Diamond problem occurs:

  • when the two parent classes have a method with the same method signature and
  • there is a conflict between two methods when a child class tries to extend these two parent classes

19. What is the alternative for multiple inheriance in Java?

Since multiple inheritance is not supported in Java, the properties and behaviour of multiple parent classes (super classes) be brought into a new class using aggregation

20. What is aggregation?

Aggregation is creating a class with instances of other classes as its members





Top Core Java Interview Questions


1. What is the difference between transient and volatile variable?

volatile variable means a way to indicate that variable needs to be always checked for the latest value instead of being fetched from a cached value. This is used when there are multiple threads and one or more threads needs to get the volatile variable.
transient variable is a variable whose value wont be serialized when the object containing it is serialized. Only memeber variables can be transient. transient is not applicable for classes, interfaces, methods and static variables.

2. What is serialization and deserialization?

Serialization is the process of converting an object into a string which can be stored in a permanent storage location accessible via a file system or database.
De-serialization is the process of converting a serialized object into its regular form.

3. What is the difference between static variable and non-static variable?

Static member variable is allocated memory only once per class. Static members can be accessed directly by using class name or by using instance variable. However, there is only one value stored per class and all instances would refer to the same value
Instance member variable is allocated memory whenever a new object is created. Non-Static members (instance members) can be accessed by using object reference

4. What is the difference between static method and non-static method?

Static method can not directly access non-static variables of the class. However an instance can be created inside the static method and variables of that instance can be accessed or modified.
Non-Static method (instance method) can be used to change static or non-static (instance) variables of the class

5. What is the difference between static nested class and non-static nested class?

Static nested class (just like static method) can not access the member variables of the enclosing class.
Non-Static nested class (inner class) is also called inner class. Inner class has access to the member variables and methods of the enclosing class (outer class)

6. What is anonymous inner class?

Anonymous inner class is a type of inner class which does not have a class name
     new Example(new Sample(){ System.out.println("Hello"); });

7. What is the difference between method overloading and method overriding?

Method overloading is writing multiple methods within the same class with method differing in its signature.

Method signature is identified by the method with

  • same name and
  • same number of formal arguments,
  • same data type of method arguments and
  • same order of data type of method arguments and
  • not differing in its return type and
  • not differing in the throws class

Method over-riding is writing methods in the subclass with the same method signature as parent class.

8. What is encapsulation?

Encapsulation is the process of hiding properties and methods inside a class so that they cannot be directly accessed or modified to avoid unexpected behaviour. Instead, access to the properties and methods are made possible via other methods which provides control over access to the data.

9. What is polymorphism?

Polymorphism is the ability to be available in multiple forms.

10. What are the types of polymorphism?

Static polymorphism and dynamic polymorphism

11. What is static polymorphism or static binding ?

Static polymorphism or static binding is compile-time polymorphism. Method overloading is an example of static binding. The method to be invoked is bound to an instance variable during compile time itself. One look at the line of method invocation itself in the source code will be able to show the method that is going to be invoked

12. What is dynamic polymorphism or Dynamic binding?

Dynamic polymorphism or Dynamic binding is runtime polymorphism. Method overriding is an example of Dynamic binding

13. What is dependency injection?

Dependency injection is the design principle by dependencies are not created by the line of code, instead created by the container and injected into the parent object.

14. What is Inversion of control?

If the decision to create objects flows from the code you write to the container or server runtime, it is regular flow of control. If the control flows from server or container to the creation of object, it is inversion of control.

15. What is inheritance?

Inheritance is the process of inheriting the properties and behaviour from another class.

public class Vehicle{
		int numberOfWheels;
		void drive(){
		}
	}
	public class Car extends Vehicle{
		double steeringWheelRadius;
		void playMusic(){
		}
	}
	

In this example, Car inherits the properties of Vehicle by extending it.

16. What is multiple inheritance?

Multiple inheritance is a class inheriting from multiple classes.

17. Does java support multiple inheritance?

Java does not support Multiple inheritance of classes

18. Why is Java not supporting multiple inheritance?

Java does not support multiple inheritance as it may give rise to the diamond problem.

Diamond problem occurs:

  • when the two parent classes have a method with the same method signature and
  • there is a conflict between two methods when a child class tries to extend these two parent classes

19. What is the alternative for multiple inheriance in Java?

Since multiple inheritance is not supported in Java, the properties and behaviour of multiple parent classes (super classes) be brought into a new class using aggregation

20. What is aggregation?

Aggregation is creating a class with instances of other classes as its members





Learn about Stack Data Structure

Learn about Heap Data Structure

Learn about Operating System

Learn AVL Tree

Learn Djikstra's Algorithm