Java Interview Questions and Answers

By IncludeHelp Last updated : March 23, 2024

This section provides set of interview questions and answers on Java programming for job seeking and experienced person that can help to find jobs and improve you technical knowledge.

What is Java?

It's a high level programming language, developed by the Sun MicroSystems in 1995. It is a fully based on OOPS (Object Oriented Programming System). Java is very reliable, secure and fast language, it is platform independent too. A Java program can be run on any system which has JVM.

What is JVM?

JVM stands for Java Virtual Machine; It is a virtual machine (abstract machine) which provides the runtime environment which Java bytecode can be executed.

What is bytecode?

Bytecode is the instruction set of the JVM, when a Java program compile, it generate an object code that is understable for the JVM i.e. we can say it's a code for JVM which is generated by the Java Complier.

What are the JDK & JRE?

JRE stands for Java Runttime Environments, it is the environment of JVM & physical existence.

JDK stands for Java Development Kit, it is the combination of JRE and Java tools, and with the help of these tools we can develop a Java Program.

What is JIT Compiler?

It is the Just-In-Time Compiler, It complies the similar functionality code also improves the performance. It reduces the compilation time as it can compile same functionality code in same time.

Is Java enables high performance and how?

Yes, Java enables the high performance as Java has Just-In-Compiler, which can compile same functionality byte code in same time. A Java program takes less time to compile than other programming language's compiler.

What is the main difference between OOPS & OOBS programming system?

The language which does not support the concept of inheritance is known as Object Oriented Based System, while OOPS supports the all features of Object Oriented Programming System including inheritance. JavaScript, VBScript are the example of Object Oriented Based System (OOBS) programming Language.

What is the most important feature of Java?

Java is basically known for the platform independency, "Java is a platform important language" It is the most important feature of Java.

What is "Platform Independency" in Java?

Here platform stands for Operating System on which program is being executed. A Java program, which is written on one platform (OS – Like Windows, Linux etc) can also run on another platform.

Which class is super class in Java Program?

Object class, is a super class in Java.

Does Java support pointers?

No, Java does not support pointers as pointer can access the memory locations directly. Hence pointers are not used in Java Programming, but there is an alternative of pointers in Java that is references. references are the different from pointers, these store the address of objects.

Why CLASSPATH and PATH are used?

These are two environment variables that are used to set an environment to run Java Programs on machine.

Write a "Hello, World!" Program in Java?

public class Main {
  private static void main(String[] args) {
    System.out.println("Hello, World!");

What is GC (Garbage Collection) in Java and how it works?

Garbage Collection collects the memory occupied the objects those are no longer in use of program, so that memory can be reclaimed or reused in the program.

Is Java supports const and how can we declare a constant?

No, Java does not support the const keyword, to declare a constant final keyword used. With the help of final keyword, we can declare a constant.

What is a final class in Java?

A class with final keyword that cannot be extended i.e. you cannot extend a final class on another class.

What will happen, if main () is declared as private?

main() method must be public, if you declared main as a private, complier returns an error “main() method not public.”

What will happen, if static modifier removed from the signature of main()?

Program compiles, but at the run time, it returns an error “NoSuchMethodError”.

What is the difference between public static void main() and static public void main()?

There is no difference between these two statements in functionality, but in respect of conventions use the visibility operators like public, private first followed by static.

How can you determine that array is null or empty?

It is very easy, you can determine it by using args.length, print the args.length, if it returns 0 , it means array is empty, if it returns an error “NullPointerException”, it means array is null.

Can a Java Program has multiple classes having main () methods?

Yes, it possible to have multiple classes with main () method in a Java Application. Because when you start a program, you have to pass class name with it. Just use that class which has main () method.

Can you have multiple main () method in a class?

No, It cannot possible, it return an error that main () has already defined.

Can I import same package/class multiple times?

Yes, you can import same package/class multiple times in your application; neither compiler not JVM will not return error. And JVM loads the package/class only once internally.

Can a top level class be private or protected?

No, a top class can never be private or protected, it always public. If you declare a top level class as either private or protected, it throws an error that private modifier cannot be used here.

Either public or no access modifier type needed with the top level class.

What is the difference between declaring and defining a variable in Java?

These two concepts are different; when you declare a variable it tells the type and name of the variable. Declaration does not occupy the memory for the variable. When you define a variable (initializing the variable), it reserve the memory bytes.


// it is just a declaration
String siteName;
// it is declaration + initialization (definition)
String siteName="";

What is serialization?

It is the mechanism by which you can save the state of an object by converting it to a byte stream. If you want to serialize the object, use serialization mechanism.

Differentiate the #include and import statement?

In C/C++ we use #include directive, it is used to make the compiler go to the standard library and copy the code from header files to our program. So that code size is increases, thus wasting memory and processor's time.

In JAVA we use import statement, it is used to make the Java Virtual Machine go to the standard library, and execute the code there, and substitute the output into our program. So that there is no need to copy the code. Hence no wastes of memory, so we can say import is an efficient than #include.

What is the main difference between float and double?

We can represent float variable value up to 7 digits after decimal point, whereas double value can be represent up to 15 digits after decimal point.

Define Unicode?

Unicode stands for Universal code. It provides a unique number for every character; it is platform and language independent. It uses 2 bytes to represent a single character.

Define the internal representation of positive and negative number.

Positive numbers are represented in binary language with the help of 1's complement and negative numbers are represented with the help of 2's complement.

Define collection?

A collection is a group of elements like integer values or objects like arrays and java.util classes (Stack, Queue, and Linked List).

Is goto statement available in java?

It is not available in java but keyword is revered for it in java. As we know that it creates confusion in large program.

Differentiate return and system.exit(0)?

As we know that return is a jumping statement which is used to transfer control from called method to calling method, whereas System.exit(0) is a method, which is used to come out of the program.

Differentiate System.out and System.err?

In java both objects are represents monitor by default. They are used to send data to the monitor. But System.out is used to print normal messages whereas System.err is used to print error messages.

What happens when String args[] is not used in main() method?

String args[] used for command line argument in java. If we will not use String args[] then it will be an compiler error. Because JVM always looks for main() method with string type array as argument.

Can a Java program have multiple classes?

Yes, A Java program can have multiple classes. But there is only single public class in a Java program.

What is a public class in Java?

A class which can be accessed anywhere outside of the package. You can also access the public class in another package.

What is a package in Java?

A package is a namespace/ folder, which contains set of related classes, interfaces and resources.

What is final class in Java?

Final class is just like constant class (since constant class is not a programming term, just for understanding), which can not be sub classed i.e. a final class can not be inherited in the program. Mostly java standard library classes are final and their all methods are implicitly final.
Example of final classes in Java API:
java.lang.String, java.lang.Math, java.lang.System

What is final, finally & finalize () in Java?

final is a keyword/modifier which is used with variable and class. Value of final variable can not be changed during program's execution, while final class can not be inherited.

finally is used with exception, it is an exception handling code section which will definitely executed whether an exception occurred or not, it is generally used to free allocated memory or resources.

finalize ():
It is a method of Object class, which is called by the garbage collector just before garbage collecting when garbage collector collecting the unused references. It is used to give a final chance to collect unused references or freeing memory.

Can an interface be final?

No, an interface can not be final, because to make interface useful we must create sub classes. Only classes can be final.

Does System.Exit() in try block executes finally block?

System.Exit() statement terminates program, so finally block will not be executed after System.Exit() statement.
Consider the example
public class JavaExp {
  public static void main(String args[]) {
    try {
      System.out.println("Try Block.");
    } catch (Exception ex) {
    } finally {
      System.out.println("Finally Block.");


Try Block.

Can we use finally block without using catch block?

Yes, we can use finally block without using catch block. try block is necessary to use finally block.
Consider the example
public class JavaExp {
  public static void main(String args[]) {
    try {
      System.out.println("Try Block.");
    } finally {
      System.out.println("Finally Block.");


Try Block.
Finally Block.

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.