Top 50 Java Interview Questions To Prepare For Your Next Interview

Top 50 Java Interview Questions To Prepare For Your Next Interview

The demand for Java skills is increasing and will continue to be in high demand, so long as the software industry continues to rely on Java-based frameworks. The modern-day technology and programming languages are always changing, as such, the skills you may have possessed this year may not necessarily be the skills that you will possess in the next.

This is why it is very important that you are aware of the latest Java interview questions and how to answer them while applying for a job. Here you will find 50 Java interview questions that are frequently asked and will empower you to prepare for some of the most in-demand developer jobs.

50 Java Interview Questions and Answers

1) What’s the Difference between JDK, JRE, and JVM? 

The Java Development Kit (JDK) is a set of tools that programmers can use to write and debug programs in the Java programming language. The JDK contains a compiler, an interpreter, a debugger, and other tools.

Java Runtime Environment (JRE) is a Java Virtual Machine. It is the runtime environment required to run Java programs and can be installed on all common operating systems. The JRE architecture is the foundation of its software development kit and can be thought of as the blueprint for all other parts that make up Java. It has a number of components, such as the JIT compiler, which enables code to execute on the fly.

Java VM is a specification that provides a virtual environment in which Java bytecode can be executed. It has become the de facto standard for most of the software and programming languages in use today. JVM follows three notations i.e. Specification, Implementation, and Runtime Instance.

2) Is Java platform independent?

This is a basic java developer interview question. Java is the leading platform for enterprise applications, and a large part of that success is due to its independence from other software and hardware systems. It can execute byte codes that work on any platform, even if the underlying operating system is different.

3) Is Java 100% object-oriented?

Java was never really an object-oriented programming language to begin with. Although it borrows some of the concepts from object-oriented languages, it is mostly a mix of procedural and object-oriented programming that developers have tolerated for years but never fully embraced. Java uses eight primitive data types. They are boolean, char, byte, short, int, long, float and double.

4) Can you use ‘this’ keyword to refer to static members?

Yes, you can use this keyword to refer to static members because it is a reference variable that refers to the current class object. But, it is not ideal to access static variables using objects.

Here is an example:

public class Test   


    static int i = 10;   

    public Test ()  




    public static void main (String args[])  


        Test t = new Test();  



5) What are Collections in Java?

A collection is a group of objects. They are used for the following operations:

  1. Searching
  2. Manipulation
  3. Sorting
  4. Insertion
  5. Deletion

6) List the classes and interfaces available in collections


  • Collection
  • Map
  • Set
  • List
  • Sorted Map
  • Sorted Set
  • Queue


  • Lists
  • Array Lists
  • Linked Lists
  • Vector


  • Hash Map
  • Hash Table
  • Linked Hash Map
  • Tree Map


  • Priority Queue

7) How would you define static and non-static methods in Java?

A method in Java is a collection of statements that perform an operation. There are two kinds of methods: static and non-static.

Static Method:

The static method or class method is common for the object of the class. This method is declared using a static keyword. This method can also be accessed using class names. Static methods cannot access any non-static instance variables and take up less space.  

Non-Static Method:

Non-static method or instance method is any method of a class that is not static. It can access both static and non-static members of the class without creating an instance of the class. This method tends to occupy more space. 

 8) Create a program to print ‘Hi Everyone’ without using the main method.

This is one of the simplest, yet popular Java interview questions.

public class PrintMessage {

    static {

        System.out.println("Hi Everyone");





//Output: Hi Everyone

9) DIfferentiate between Heap and Stack Memory in Java.

This is one of the most frequently asked Java interview questions.

Memory in a Java program can be allocated on a heap or stack. 


Objects stored in heap can be accessed globally. It is used by all parts of the application and lives from the start till end of the application’s execution. Every time an object is created, it is stored in the heap space. Heap comes with great storage space.


Stack is used only by one thread of the execution. Stack memory is considered safer because, unlike heap memory, stack cannot be accessed globally. It can only be accessed by the owner thread. This memory comes with less storage space compared to heap memory.

10) Why are pointers not used in Java?

The most remarkable feature of Java is its simplicity. Pointers are unsafe and can increase the complexity of your program. 

11) What is a class in Java?

A class is a blueprint that constitutes all your data. It has variables and methods that one can use to describe an object’s behavior. 

12) What do you know about an infinite loop in Java?

An infinite loop is a sequence of instructions that keeps looping when a functional exit is not met. It could either be an error in your programming or a deliberate action based on the behavior of your application. It terminates on its own once the application exits.

13) What is the output of this program?

This question is one of the most common java coding interview questions.

public class Test   


    Test(int a, int b)  


        System.out.println("a = "+a+" b = "+b);  


    Test(int a, float b)  


        System.out.println("a = "+a+" b = "+b);  


    public static void main (String args[])  


        byte a = 15;   

        byte b = 30;  

        Test test = new Test(a,b);  



Output: a = 15  b = 30

14) Write a program to find the sum of two numbers.

This is one of the most popular java interview questions. Here is how you can find the sum of two numbers.

public class SumClass {

    public static void main(String args[]) {

        int x=5;

        int y=30;

        int z=x+y;

        System.out.println("Sum of x+y = " + z);




Output: 35

15) What is constructor chaining in Java?

The process of calling one constructor from another constructor is called constructor chaining in Java. This process is accomplished through inheritance. It can occur either within the same class or from the base class. Constructor chaining is performed to perform several tasks in a single constructor without having to create a code for each task. 

16) Are Java strings immutable? Explain.

Yes, in Java, string objects are immutable. When a certain string object is immutable, it simply means that its state is not modifiable post creation. That is, once an object is created and assigned to a variable, you cannot mutate its internal state or update the reference. STring is kept immutable in Java to reinforce security, increase performance, and maintain synchronization.

But if strings were mutable, you would have no methods to verify the security of the string you receive. It could even subject your query to SQL injections.

17) Define polymorphism

Polymorphism is a concept in Java with which one can perform one action in multiple ways. You can assign a different usage or meaning to something in varying contexts. There are two types of polymorphism in Java:

  1. Compile-time polymorphism

Compile-time polymorphism, also known as static binding, is a polymorphism that occurs when an object is bound by its functionality at the compile time. This polymorphism is achieved using method overloading.

           2. Runtime Polymorphism

Runtime polymorphism, also called late binding, is a polymorphism that occurs when an object is bound by its functionality at the run time. This polymorphism is achieved using method overriding. 

18) What do you know about abstraction in Java?

Abstraction is the property that helps a program display only the essential details to the user. It is the process of identifying only the most important details of an object and ignoring the non-essential elements. This is achieved using interfaces and abstract classes. Abstraction minimizes the complexity in the process of viewing things. It increases the security of an application and avoids code duplication. 

19) Can you execute method overloading by changing the return type?

No, due to ambiguity, method overloading is not performed by changing the return type

For example,

class Adder{  

static int add(int a,int b){return a+b;}  

static double add(int a,int b){return a+b;}  


class TestOverloading3{  

public static void main(String[] args){  




Output: Compile Time Error : method add (int, int) is already defined in class Adder

20) What is inheritance?

In Java, the properties of one class can be inherited by another. This helps create reusability of code and establishes a relationship between various classes. Inheritance occurs between two kinds of classes:

  1. Parent class (Super class or Base class) - the class from which properties are inherited
  2. Child class (Subclass or Derived Class) - the class that inherits properties

21) List the different kinds of inheritance in Java

Java enables four kinds of inheritance. They are:

  1. Single Inheritance - There is only one parent class and one child class.

       2. Multilevel Inheritance - A child class has multiple parent classes.

       3. Hierarchical Inheritance - A class has more than one child classes

       4. Hybrid Inheritance - This is a combination of two or more kinds of inheritance.


22) What is the output of this Java program?

class Test   


    public static void main (String args[])   


        for(int i=0; 0; i++)   


            System.out.println("Hello Javatpoint");  




Output: The output will show compile-time error. The loop requires a boolean value and the integer value is 0 in the second part.

23) Define aggregation in Java

Aggregation is used to define the one-way association between two objects. It is a special form of association in which all objects have their own life cycles but the child object cannot belong to another parent object. 

24) What is composition?

A composition is a strong kind of aggregation that also goes by the name ‘death relationship’. In this form of aggregation, the child object does not have its own lifecycle. One class depends on another and cannot function in the absence of the other class. 

Composition is flexible, you can alter class implementation at runtime by changing the object and its behavior. If you are into test-driven development, composition comes in handy as it offers better testability.

25) Explain servlet

A servlet is a programming language class in Java that can extend the capabilities of servers. It offers a platform-independent, component-based method for creating web-based applications. It provides support for data persistence and dynamic response. 

You can use servlets to procure inputs from users, create dynamic web pages, and get records from a database. It boosts the performance of your application and increases safety. The best part is you do not have to create a separate process to handle the requests of each client. 

26) Given below is a Java program. What would be its output?

class Test   


    int i;   


public class Main   


    public static void main (String args[])   


        Test test = new Test();   





The output is 0. The variable ‘i’ is internally initialized to 0. If there is no constructor in the class, a default constructor is invoked implicitly. Variable ‘i’ is initialized due to the absence of a constructor. 

27) When would you use a runnable interface and a thread class in Java?

This is an important java programming interview question that scales your judgement skills. A runnable interface is employed when you need your class to extend classes other than the thread. In Java, one can extend only a single class.

A thread class is used when there is no requirement to extend your class.

28) Why should you use a character array for storing sensitive or confidential information?

Strings in Java are immutable. That is, strings cannot be altered. They continue to stay in the string pool until they get removed. In short, they can stay in the heap section for an unregulated period of time. If your memory is accessed by a hacker, your vital information could be stolen. This is where the character array steps in; it is a mutable object that can be used to store any variable. It saves heap memory and protects your valuable data. 

29) Is synchronization necessary?

Synchronization in Java is an essential process. It enables you to execute multiple processes concurrently. You might encounter situations where multiple threads require the same resource. Synchronization helps you tackle this issue; by synchronizing, you enable only one thread to access a shared resource at a time. 

30) What are the different stages in the life cycle of a servlet?

There are 5 stages in the life of a servlet:

  1. Loading
  2. Instantiation
  3. Initialisation
  4. Request
  5. Destruction

31)  Illustrate ‘static variable’ with an example.


//Program of static variable  


class Student6{  

   int rollno;  

   String name;  

   static String college ="ITS";  


   Student6(int r,String n){  

   rollno = r;  

   name = n;  


 void display (){System.out.println(rollno+" "+name+" "+college);}  


 public static void main(String args[]){  

 Student6 s1 = new Student6(111,"Abhinav");  

 Student6 s2 = new Student6(222,"Nina");  






32) What is a JDBC Driver?

JDBC (Java Database Connectivity) driver in Java is a software component that employs the JDBC API to facilitates its interaction with the database. It allows a Java application or program to access a wide variety of databases and run on any platform.

There are 4 types of JDBC Drivers:

  1. JDBC-ODBC bridge driver
  2. Native-API driver (partially java driver)
  3. Network Protocol driver (fully java driver)
  4. Thin driver (fully java driver)

33) What is a Java package? List the advantages of packages.

A package in Java is a group of similar kinds of classes, sub-packages, and interfaces. They are of two types: built-in package and user-defined package. Some of the core packages in Java include Java.lang,, Java.util, and

To create a package in Java, a package keyword is used. Below is an example of a Java Package:

//save as  

package mypack;  

public class Simple{  

public static void main(String args[]){  

    System.out.println("Welcome to package");  



The following are the advantages of packages:

  • Packages give you easier access control
  • With packages, it is easy to locate related classes.
  • Packages help you avoid name clashes.
  • It is possible to maintain hidden classes.

34) What do you know about object cloning in Java?

Object cloning is the process of creating an exact replica of an object. The replica should have a similar state as that of the object. Cloning is performed in Java using the method clone(). This method builds a new instance of the current object’s class and initializes all fields with the same contents. 

35) How do cookies function in servlets?

Cookies are text data passed from the server to the client that gets saved on the client local machine. Cookies support is provided by the servlet API via javax.servlet.http. HttpServletRequest getCookies() is used to obtain the cookies from the request. To attach the cookie in the response header, HttpServletResponse addCookie(Cookie c) method is used.

36) We can declare static variables and methods in an abstract class. Support this statement with an example.

It is possible to declare static variables and methods in an abstract class. You do not need an object to access the static context. That is, one can access the static context that is declared inside the abstract class using the name of the abstract class. 

abstract class Test  


    static int i = 102;  

    static void TestMethod()  


        System.out.println("hi !! I am fine !!");  



public class TestClass extends Test   


    public static void main (String args[])  



        System.out.println("i = "+Test.i);  



The output of the program is      hi!! I am fine !!

                                                          i = 102

37) What do you know about wrapper classes in Java?

The Java language has a series of wrapper classes which convert the primitive data types into object references, so that they can be used interchangeably. The primitive data types are all represented by objects, and are organised by their data type. These are known as wrapper classes because they wrap the primitive data type into an object of that class. Wrapper classes often come with additional methods for manipulating the data in a more efficient manner than a raw primitive.

38) Demonstrate constructor chaining with ‘this’ keyword. 

public class Student  


    int id,age;   

    String name, address;  

    public Student (int age)  


        this.age = age;  


    public Student(int id, int age)  


        this(age);  = id;  


    public Student(int id, int age, String name, String address)  


        this(id, age);   = name;   

        this.address = address;   


     public static void main (String args[])  


        Student stu = new Student(105, 24, "Karan", "Delhi");  

        System.out.println("ID: "" Name:"" age:"+stu.age+" address: "+stu.address);  




Output - ID : 105 Name: Karan age: 24 address: Delhi


39) Would you use both ‘this()’ and ‘super()’ in a constructor? 

This is one of the most popular Java basic interview questions. 

No, it is impossible to use this() and super() in a constructor because both of them are to be used as the first statement in the class constructor.

For example,

public class Test{  





         System.out.println("Test class object is created");  


     public static void main(String []args){  

     Test t = new Test();  



The output is : 5:  error  : call to this must be first statement in constructor

40) What is association?

Association is the relationship between two classes that are set up through objects. The association could be one-to-one, one-to-many, and many-to-many. Association defines how objects communicate with each other.

41) What would be the output of this program?

class Test   


    int test_a, test_b;  

    Test(int a, int b)   


    test_a = a;   

    test_b = b;   


    public static void main (String args[])   


        Test test = new Test();   

        System.out.println(test.test_a+" "+test.test_b);  



In this program, there is a compiler error. In the main method, there is a call to the default constructor, which is absent in the class. But there is only a single parameterized constructor in the class Test. As a result, no default constructor is implicitly invoked.

42) What is a default constructor used for?

This is a commonly asked java interview question for experienced. A default constructor is used to assign default values to objects. Java compiler implicitly creates a default constructor if the class is devoid of a constructor.

class Student3{  

int id;  

String name;  


void display(){System.out.println(id+" "+name);}  


public static void main(String args[]){  

Student3 s1=new Student3();  

Student3 s2=new Student3();  





Output: ∅  null

               ∅  null


43) What is encapsulation?

Encapsulation in Java is a mechanism where the variables and methods are bound together as a single unit. The data is hidden from third parties and can be accessed only using current class methods. You can use encapsulation to protect your data from unnecessary modifications. 

In Java, you can employ either of the following methods to perform encapsulation:

  1. Providing public setter or getter methods
  2. Declaring the variables as private

44) Can you declare an interface as final?

No, one cannot declare an interface as final. An interface should be implemented by a class to give its definition. Even if you attempt to make it final, the compiler will show error. 

45) What are constructors in Java? Explain the different types.

Constructor is a block of code used to initialize an object. It is mandatory for them to have the same name as that of the class. 

There are two types of constructors:

  1. Default Constructor

A default constructor does not take any inputs. It is a no-argument constructor which is created by default when the user does not define any other constructor. It is primarily used for object creation.

  1. Parameterized Constructor

Parameterized constructors can initialize instance variables with the values provided. These constructors take arguments.

46) What do you know about the Java String Pool?

Java String Pool is a collection of strings that are allocated on the heap memory. Every time you create a new object, the string pool verifies whether it already exists in the pool. If it does, the same reference is returned to the variable. If not, the new object will be created in the string pool.

47) Can you declare a constructor as final? 

No, you cannot. Constructors are not by any means ordinary methods. If you try to make them final, the compiler will show error.

48) What is the output of this program?

class Test   


    public static void main (String args[])   


        System.out.println(10 + 20 + "Javatpoint");   

        System.out.println("Javatpoint" + 10 + 20);  




Output: 3ØJavatpoint


49) Is ‘constructor’ inherited? 

No. Constructors are not members. They are not inherited by subclasses.

50) What do you know about the final keyword in Java?

A final keyword is a special keyword that is used as a non-access modifier in Java. Its usage fits the following contexts:

Final Method

A final method is final, an inheriting class cannot override it. 

Final Class

When a class is declared final, it cannot be extended by a subclass but only by a different class.

Final Variable

When you use a final keyword with a certain variable, its value cannot be altered once assigned. 

Web development is one of the most happening fields in the tech sector. With the competition growing intense, the very idea of appearing for interviews might seem daunting to you. But fret not. The prospect of a great job is worth all the trouble you take to prepare for your interview. Refer to the aforementioned core java interview questions, to prepare well for your interview.