Java Interview Questions

Read other articles of this series

In this post, we are covering some of the important Java interview questions for your next interview.

Introduction

Preparing for a Java interview is not an easy task. You may face many different problems starting from simple beginner level questions to the core concept of different Java classes.  In this tutorial, we are listing mostly asked Java interview questions and answers. There is no way of guaranteeing what type of problems you may face in an interview but we hope that this blog post will help you to handle a good number of Java questions :

 

Q1. What are JDK, JRE, and JVM?

JDK: JDK is the acronym for Java Development Kit. This is the development kit for Java application development and debugging. It is platform specific. Means, we have different installers for different operating systems.JDK includes different utilities required for Java application development like Java virtual machine, libraries, development tools like javac, documentation, debugger etc.

JRE: JRE is the acronym for Java Runtime Environment. JRE is inside JDK. It provides a runtime environment used to execute a Java program.JRE includes JVM, libraries and a few other classes that needed to execute a Java program. Note that JDK is to develop a Java program and JRE is to run a Java program.

JVM: JVM is the acronym for Java Virtual Machine. It is called “virtual” because JVM doesn’t physically exist. It converts the Java bytecode to machine language. Java compiler compiles the “.java” files to “.class” that contains the bytecode for JVM.

 

Q2. Why Java is not a fully object-oriented programming language?

A programming language is a fully object-oriented programming language if everything in a program is an object. But in Java, all primitive types like char, byte, boolean, short, int, double etc. are not objects. These are predefined types. So, we can’t say Java as a pure or fully object-oriented programming language.

 

Q3. What do you understand by platform independence? Do you think Java is platform independent?

Platform independence means once we compile the code for a program, we can run it on any operating system.Java is platform independent. Because Java compiler compiles the java files to bytecode. On any platform, JVM can execute the same bytecode without recompiling. Note that JVM is platform dependent, i.e. for each OS, we have different JVM installed. But each JVM can execute the same bytecode compiled on any operating system.

 

Q4.What are the class and object?

A class is a template that holds different information like data types and methods used by the objects. An object is a specimen of a class which consists of all data types and methods defined in the class. We can create multiple objects with the same class.

 

Q5. What is a constructor?

A constructor is a method that executed when an object is created. We can have multiple constructors for a single class and at least one of the constructor will invoke while creating a new object. The name of the constructor is the same as the name of the class.

 

Q6. What is an Anonymous inner class in Java?

Anonymous inner class is an inner class declared and instantiated at the same time. We can use the ‘new’ operator to instantiate and initialize it. e.g. :

class Vehicle {
 interface Car {
  public void numberOfDoors();
 }
 public static void main(String...args) {
  Car car = new Car() {
   public void numberOfDoors() {
    System.out.println("4");
   }
  };
  car.numberOfDoors();
 }
}

 

Q7. What is an inner class in Java?

Inner class, as its name suggests defined inside another class. The main difference between outer class and inner class is that an inner class can be private, default, public or protected but an outer class is only public or default. Example :

class Vehicle {
 class Bus {
  public void numberOfDoors() {
   System.out.println("2");
  }
 }
 public static void main(String...args) {
  Vehicle vehicle = new Vehicle();
  Bus bus = vehicle.new Bus();
  bus.numberOfDoors();
 }
}

 

Q8. What is an object class?

By default, all classes inherit the object class directly or indirectly. If a class is not extending any other class, it inherits the object class directly. If it is extending another class, this class is inherited from the object class indirectly. Object class methods are available to all classes.

 

Q9. What is a singleton class?

Singleton is a design pattern used to create a class with only one single instance, i.e. it will instantiate only once in the Java Virtual Machine. These classes are useful for utility or common methods like logging, configuration etc. The constructor of the singleton class is private. One different static method is created inside the class to get the instance of the class.

public class AppUtil {
 private static AppUtil instance;
 private AppUtil() {}
 public static AppUtil getInstance() {
  if (instance == null) {
   synchronized(AppUtil.class) {
    if (instance == null) instance = new AppUtil();
   }
  }
  return instance;
 }
}

Here, AppUtil is a singleton class. It has only one instance. We can get this instance using the static method “getInstance” from any other class.

 

Q10. What is an abstract class?

Abstract class in Java denoted by the “abstract” keyword. We cannot instantiate an abstract class. An abstract class may contain abstract or non-abstract methods. If a class has an abstract method, it should be abstract. Like other classes, an abstract class can have constructors. Abstract classes are used mostly as Base classes with common methods and method definitions for all subclasses.

 

Q11. What is an interface?

An interface is a way to meet abstraction in Java. The interface has only method bodies, not implementation. If a class is implementing an interface, it needs to complete the implementation for all methods defined in the interface. An interface provides polymorphism in Java. The interface can also contain default methods, nested types, static methods, and constants. For default methods and static methods, we can implement bodies inside the interface. It doesn’t contain any constructor and we can’t instantiate it. A class can implement multiple interfaces at the same time.

 

Q12. What are the differences between abstract class and interface?

  • “interface” keyword is for declaring interfaces but the “abstract” keyword is to declare an abstract class.
  • The interface supports multiple-inheritance but abstract class doesn’t support multiple-inheritance.
  • A class implements an interface but a class extends an abstract class.
  • All members of an interface are public but members of an abstract class can be private, protected etc.
  • an interface can’t have any constructors but an abstract class can have constructors

 

Q13. What is a default method?

Default method got in Java 8. Before Java 8, interfaces could contain only abstract methods. Only the classes implementing these interfaces can have the implementation code. Starting from Java 8, we can also add methods with implementation in interfaces. The implementation of these methods in the class is optional.

 

Q14. How sub-class is different from inner class?

  • The inner class is in the same file as the parent class but a subclass can be placed in a separate file.
  • To get the instance of an inner class, we need the instance of the parent class first. But for a subclass, we can create an instance directly.
  • To get the instance of an inner class, we need the instance of the parent class first. But for a subclass, we can create an instance directly.

 

Q15. What are the different features of Java 8?

Please read the New Features in Java 8  for the detail

 

Summary

In this post, we covering some of the important Java interview questions. We wish you the best of luck for your interview and hope that these Java interview questions will help you in your next interview.

Scroll to Top