Back to Blog
March 31, 2022
Deepthy Prabha
en

Top 50 Java Interview Questions To Prepare For Your Next Interview

Full Stack
Interview Tips
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 ()  

    {  

        System.out.println(this.i);      

    }  

    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

Interfaces:

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

Classes:

  • Lists
  • Array Lists
  • Linked Lists
  • Vector

Maps:

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

Queue:

  • 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");

        System.exit(0);

    } 

}

 

//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. 

Heap:

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:

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){  

System.out.println(Adder.add(11,11));//ambiguity  

}}  

 

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();   

        System.out.println(test.i);  

    }  

}  

 

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;  

Related Articles