Top Java Interview Questions and Answers

Even today, Java is one of the most popular programming languages in use according to GitHub, particularly for client-server web applications, with a reported 9 million developers. Below is a Comprehensive list of most frequently used Java Interview Questions.

What is Java?

Answer: Java is an Object Oriented Programming language developed by Sun Micro Systems. It was developed with an intention to to be platform independent – meaning the application developed using Java can run on any platform.

What are the main features of Java?

Answer: The main features of Java are

  • Object Oriented – OOPs concepts provides us the ability to encapsulate data and functional logic into classes which in turn enables us to solve complex real world problems with well designed scale-able solutions.
  • Platform Independent – Same Program/Application can be deployed and run in any platform.
  • High Performance – JIT (Just In Time compiler) feature in Java enables high performance. JIT converts the bytecode into machine language and then JVM performs the execution.
  • Multi-Threaded – Java provides us the ability develop multi-threaded applications which can execute tasks in parallel.

What are the code editors or IDEs available for Java?

Answer: There are many code editors and IDEs(Integrated Developer Environment) available for Java today. The most popular ones are EclipseIntelliJVisual Studio Code etc.

What is a Class?

Answer: Class is a collection of variables and functions which can represent any real world Object. It can also be called as a Template of an Object.

For example Car, Bike, Person etc

public class Car{ //Class name declaration
String make = "Toyota"; //Variable declaration
int model = 2020;
int price;
public void setPrice(int price){ //Method declaration
this.price= price;
}
}

Programs in Java are written as Classes.

What are variables?

Answer: Variables are also called as attributes or properties which defines the state of a class.

What are methods?

Answer: Methods are also called as functions which wraps the code basically the business logic which acts upon the data which are stored in the variables.

What is an Object?

Answer: Object is an instance of a Class which will have a state and behavior

public class Car{
public static void main(String[] args){
Car myCar = new Car();//Object creation
}
}

The above code creates an Object for the Car class, whenever the JVM finds the new keyword then it will create an instance of that Class.

What is the difference between the Local Variable and Instance variable?

Answer: Local Variables – are the variables that are declared inside the functions, these variables cannot be accessed out side these functions. Basically the local variables scope is only within the functions they are defined.

Instance Variables – Instance variables are nothing bu the attributes or properties defined in the Class and outside the functions. These variables defines the state of a Class. Scope of these variables exists through out the Class, basically all the functions can have access to these variables.

What is a Constructor in Java?

Answer: A Java Constructor is a special method inside the class which will be called while instantiating an Object. Below are some of the characteristics of a Constructor,

  • Constructor will have the same name as Class
  • There can be multiple Constructors for a Class based on the need
  • If there is no Constructor defined explicitly the Java Compiler will provide a public no-argument constructor to a Class
  • If the user wants to use the Constructor then he should start with defining the default Constructor – public no-argument Constructor then they can add additional Constructor with Overloaded arguments.

What is Inheritance?

Answer: Inheritance is one of the most important concepts in OOPs. Using Inheritance we can derive one Class from another Class, basically we can bring the variables and functions from One Class into another Class, this way we can avoid the code duplication and achieve re-usability.

The existing Class is called as a Super Class and new Class will be called as Sub Class.

Example-
Super class:
public class Automobile(){
}
Sub class:
public class Car extends Automobile(){
}

Inheritance is applicable only for public and protected members. Private members can’t be inherited. 

What is Encapsulation?

Answer: Encapsulation is one of important concept of OOPs. Encapsulation is a mechanism of wrapping the data and code that manipulates the data into a single unit. In Java this is achieved by writing the Program(code) in the form of Classes which wraps variables(data) and functions(code) into one unit.

What are the benefits or advantages of Encapsulation?

Answer: Encapsulation provides the below benefits

  • It protects the data and code
  • Enables Proper access mechanism for data and code
  • Improves code re-usability and maintainability
Share this
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Leave a Reply