Top Interview Question of Java13 min read

This article contains 50+ top java interview questions and answers that help you prepare for your interview. These are frequently asked java questions in an interview from basic to intermediate.

This article will help to understand the OOP concept, basic overview, exceptions, threads, modifiers, collections, Bytecode, various differences, etc. and help to get ready for any java related interview.

1. What is Java?

Java is a popular general-purpose, high-level, modern programming language, and computing platform. It is fast, reliable, secure and dynamic, with the ability to fit the needs of virtually any type of application. It was created by James Gosling from Sun Microsystems (Sun) in 1991. It is Platform Independent, which means the user only needs to write a program once and it can be run on a number of different platforms such as Windows, Mac OS, and the various versions of UNIX.

2. What are the features of JAVA?

  Platform Independent
  Simple and secure
  High Performance
  Compiled and Interpreted
Click Here to learn in detail.

3. What do you understand by JVM?

JVM stands for Java Virtual Machine, which is an abstract machine that provides a runtime environment to run Java programs. JVM is the specification that must be implemented in the computer system. It converts Java bytecode into machine language, so not only Java but it also enables the computer to run other programs that are compiled to bytecode. There are three notations in JVM:
Specification, Implementation, and Runtime Instance.

4. Name the types of memory allocated by JVM?

Types of memory allocated by JVM are:

  • Method Area:
    In method area, structures like the run-time constant pool, field and method data, variables information, static variables, etc. are stored.
  • Heap:
    It is a shared resource among all the threads and created during run-time. All the Objects, metadata, and arrays are stored in the heap.
  • JVM language Stacks:
    Java language Stacks stores parameters, local variables and return addresses during method calls. It is not a shared resource. One JVM stack is created simultaneously for each thread that is each thread contains its own JVM stack.
  • PC Registers:
    PC(Program Counter) Register contains/stores the address of JVM(Java Virtual Machine) instruction that is being currently executed. Each thread in Java has its own PC register.
  • Native Method Stacks:
    Every thread has its own separate native stack hold the native method information.

Click here for more detail

5. What is the Java IDE’s?

Eclipse and NetBeans are the IDE’s of JAVA.

6. JDK vs JRE vs JVM.


It stands for Java Development Kit. It is the tool necessary to compile, document, and package Java programs. It contains JRE + development tools and is an implementation of any one of the following Java Platforms released by Oracle:
1. Standard Edition Java Platform
2. Enterprise Edition Java Platform
3. Micro Edition Java Platform


It stands for Java Runtime Environment. JRE refers to a runtime environment in which Java bytecode can be executed and used for developing java applications with a set of libraries. It’s an implementation of the JVM and is physically exists.


JVM stands for Java Virtual Machine, which is an abstract machine that provides a runtime environment to run Java programs. JVM is the specification that must be implemented in the computer system. There are three notations in JVM:
Specification, Implementation, and Runtime Instance.

7. What is the JIT compiler?

Just-In-Time(JIT) compiler: It is used to increase the performance of the compiler by taking the block of similar bytecode that is the repeated method calls. Hence reducing the compilation time and making it more efficient.

8. What is meant by the Local variable and the Instance variable?

Local variable: A variable that is declared within the body of the method or constructor is called a local variable. And is destroyed after exiting the method or block.
Instance variable: These are the non-static variable that is declared within the class but outside the method or constructor. And can be accessed only by creating objects.

9. What gives Java its ‘write once and run anywhere’ nature?

The bytecode. Java compiler converts the Java programs into the class file (Byte Code) which is the intermediate language between source code and machine code. This bytecode is not platform-specific and can be executed on any computer.

10. What are Class and Objects?

Class: A class is a user-defined blueprint or prototype for creating Objects. It is a logical entity.
A class in Java contain:

Objects: It can be defined as a real-world entity that has state and behavior. An object is a runtime entity. Eg. bike, Lamp, pen, table, etc.
An Object is an instance of a class. It is a physical as well as a logical entity.

11. What are the Oops concepts?

12. What is the classloader subsystem?

This subsystem is used for loading class files. It performs three major functions :
Loading, Linking, and Initialization.
There are three built-in classloaders in Java:

  • Bootstrap ClassLoader: It loads the rt.jar file which contains all class files of Java Standard Edition like java.lang package classes, package classes, java.util package classes.
  • Extension ClassLoader: It loads the jar files located inside $JAVA_HOME/jre/lib/ext directory.
  • System ClassLoader: It loads the class files from the classpath. Also known as Application classloader.

13. Does Java Use Pointers?

No, Java doesn’t use pointers. It has tough security and is not safe. Instead of pointers, java uses reference types as they are safer and more secure. Memory allocation is done automatically so to avoid direct access to memory by the user java doesn’t allow pointers.

14. What are the various access modifiers for Java classes?

The access modifiers in Java specify the accessibility or visibility or scope of a field, method, constructor, or class. Following are Java’s four access modifiers with their accessibility:

  • default (Same class, Same package)
  • private (Same class)
  • protected ( Same class, Same package, Subclasses)
  • public ( Same class, Same package, Subclasses, Everyone)

15. What are the Functions of JVM and JRE?

JVM (Java Virtual Machine) provides a runtime environment for Java Byte Codes to be executed.
JRE (Java Runtime Environment) includes sets of files required by JVM during runtime.

16. How can we restrict inheritance for a class?

We can restrict inheritance for a class by the following steps:

  1. By using the final keyword.
  2. If we make all methods final, then we cannot override that.
  3. By using private constructors.
  4. By using the Javadoc comment (//)

17. What Is the Difference Between Overloading and Overriding?

Method Overloading is one of the features in a class having more than one method with the same name. Method Overriding is a feature that allows the user to declare a method with the same name in a sub-class that is already present in a parent class.

18. What is the super keyword in java?

The super keyword in Java is used in the sub-class for calling the inherited parent class method/constructor. Super is used to access superclass variables, methods, constructors.
Super can be used in two forms :

  1. The first form is for calling a superclass constructor.
  2. The second one is to call superclass variables, methods.

19. Why java is platform-independent?

Unlike other programming languages such as C, C++, etc, which require a specific platform to be compiled whereas Java is compiled into byte-code and this byte-code is Platform Independent. This means Java is only required to write-once and can be run on any platform such as Windows, Linux, Mac OS, etc. This byte-code is interpreted by the Java Virtual Machine (JVM).

20. What is bytecode in java?

When a javac compiler compiles a class it generates a .class file. This .class file contains a set of instructions called byte code. Byte code is a machine-independent language and contains a set of instructions that are to be executed only by JVM. JVM can understand these byte codes.

21. Explain the main() method in java?

The main () method is the starting point of execution for all java applications.

public static void main(String[] args) {}

String args[] are an array of string objects we need to pass from command line arguments. Every Java application must have at least one main method.

22. Difference between ‘>>’ and ‘>>>’ operators in java?

>> is a right shift operator that shifts all of the bits in value to the right to a specified number of times.
int a = 12;
a = a >> 3;

The above line of code moves 12 three characters right.
>>> is an unsigned shift operator used to shift right. The places which were vacated by shift are filled with zeroes.

23. What Is a Package?

Packages in Java are a way to pack(group) a number of related types such as classes, interfaces, enumerations, and annotations together into a single unit. Packages are used to avoid name conflicts, provide a good structure to the projects with maintainable code that consists of many classes.

24. What is a singleton class?

A singleton class in java can have only one instance and hence all its methods and variables belong to just one instance. Singleton class concept is useful for situations when there is a need to limit the number of objects for a class.

25. What are Loops in Java?

loop statement allows us to execute a block of code or statement or group of statements as many times according to the user’s need. It is done by evaluating a given condition for true and false. The statement stops looping when the condition is false.

26. What is an infinite Loop and how it is declared?

An infinite loop runs without any condition and runs infinitely. An infinite loop (sometimes called an endless loop).
An infinite loop is declared as follows:

27. Java doesn’t support Multiple Inheritance. Why?

In this type of inheritance, one class(derived class) can inherit from more than one superclass. Java doesn’t support multiple inheritances because the derived class will have to manage the dependency on two base classes. It creates ambiguity.

28. Define an abstract class.

An abstract class is a class that is declared with an abstract keyword. An abstract class may or may not have abstract methods. This class cannot create objects, to access this class, it must be inherited. This class can have public, private, protected, or constants and default variables.

abstract class ClassName{ }

29. What is the finalize method?

Before destroying an object, the finalize() method is called by Garbage Collector for performing the cleanup process. And once it’s done, Garbage Collector destroys that object. The finalize() method is present in an Object Class as:

protected void finalize(){}

30. What is Runtime Exceptions?

Unchecked Exceptions are also known as run-time exceptions. These exceptions occur during the time of execution of a program that is at run-time. Some of the Unchecked Exceptions occur are ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException, etc.

31. What is an Interface?

An Interface in Java is the same as a class, like any other class, an interface can have methods and variables. But unlike a class, the method in it is an abstract method (only method signature, contain no-body), and also by default, the declared variables in an interface are public, static & final. It is used to achieve abstraction and loose coupling.

32. What is an applet?

The Applet in Java is a special type of internet-based program, that runs on the web browser and works at the client side. It is used to make the webpage more dynamic and provide interactive features that cannot be applied by HTML alone. Any applet in Java is a class that extends the java.applet.Applet class. An Applet class does not contain the main() method.

33. Why Multiple Inheritance is not supported in java?

In this type of inheritance, one class(derived class) can inherit from more than one superclass. Java doesn’t support multiple inheritances because the derived class will have to manage the dependency on two base classes. In java, we can achieve multiple inheritances only through Interfaces. It is rarely used because it creates an unwanted problem in the hierarchy.

34. What is the difference between StringBuffer and String?

A string is an Immutable class that is their values are unchangeable once it is created. Whereas StringBuffer is a mutable class that is their values or content can be changed later. Every time we alter the value or content the String objects create a new string rather than changing the existing one. Due to this, the StringBuffer is preferred rather than String.

35. Distinguish between StringBuffer and StringBuilder in Java programming.


  • StringBuffer is a thread-safe.
  • These methods are synchronized.
  • The performance is very slow.


  • StringBuilder is fast as it is not thread-safe.
  • These methods are non-synchronized.
  • The performance is very fast.

36. What are the default value of float and double data-type in Java?

For float its 0.0f and for double it’s 0.0d

37. What is an Exception?

An exception in Java is an event that arises during the execution of a program i.e. during run-time. The occurrence of an exception in a program disrupts the normal flow of instructions. This causes the termination of the program or application and therefore needs to be handled by calling an exception handler, which deals with the exception.

38. When a super keyword is used?

The super keyword in Java is used in the sub-class for calling the inherited parent class method/constructor. It can be also used to refer to a hidden field.

39. What is the benefit of using Encapsulation?

Benefits of Encapsulation:

  • Data- hiding in Java.
  • The class field can be made read-only or write-only.
  • It provides the class the total control over the data.

40. Why Packages are used?

Packages are used to avoid name conflicts, provide a good structure to the projects with maintainable code that consists of many classes.

41. State the main purpose of using MultiThreading.

The main purpose of MultiThreading is to achieve the simultaneous execution of more than two tasks so to maximize the use of CPU time.

42. What are the ways in which Thread can be created?

There are two ways to create a thread:
  By extending Thread class.
  By implementing the Runnable interface.

43. name a class extended by applet?

An applet extends java.applet.Applet class.

44. What is Garbage collection in Java?

Garbage collection in java is a way in which the unreachable objects are destroyed to free up space in memory. JVM creates a heap area which is called runtime data area, where all the objects are stored. And the space in that area is limited so it is needed to manage this area efficiently by removing the objects that are no longer required or are unused. And therefore, this process of removing the unused objects present in the heap area is known as Garbage collection.

45. Define final keyword in java.

In java, the final keyword is used in many contexts to identify the entity that can only be assigned once and is used as a non-access modifier. These can be:

final variable:
The variable declared as final cannot have its value changed once it is assigned. It remains the same that is constant.
final int maxSpeed = 90; //final variable  

final method:
The declared final method cannot be overridden by the inheriting class.
final void speed() { block of code }

final class:
If a class is declared as final, it cannot be extended by other subclasses. Although, it can extend to other classes.
final class Speed { code }

More will be added soon. it will be updated from time to time, so stay connected to simple2code.


C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …