Most Important Java Interview Questions and Answers

Software Testing Sapiens
5 min readApr 13, 2022

1) what are static blocks and static initializers in Java?

In most of java interview questions commonly asked question, Static blocks or static initializers are used to initialize static fields in java.

we declare static blocks when we want to initialize static fields in our class.

Static blocks get executed exactly once when the class is loaded Static blocks are executed even before the constructors are executed.

2) How to call one constructor from the other constructor?

With in the same class if we want to call one constructor from other we use this() method.

Based on the number of parameters we pass appropriate this() method is called.
Restrictions for using this method :
1) this must be the first statement in the constructor
2)we cannot use two this() methods in the constructor

3) What is method overriding in java?

If we have methods with the same signature (same name, same signature, same return type) in the superclass and subclass then we say
subclass method is overridden by the superclass.

When to use overriding in java?
If we want the same method with different behavior in superclass and subclass then we go for overriding.
When we call overridden method with subclass reference subclass method is called hiding the superclass
method.

Method Overriding and Method overloading important java programming interview questions mostly asked in interviews

4) What is a super keyword in java?

Variables and methods of the superclass can be overridden in a subclass.

In case of overriding, a subclass object calls its own variables and methods.

A subclass cannot access the variables and methods of superclass because the overridden variables or methods hide the methods and variables of the superclass.

But still, java provides a way to access superclass members even if its members are overridden. Super is used to access superclass variables, methods, constructors.
Super can be used in two forms :
1) the First form is for calling the superclass constructor.
2) the Second one is to call superclass variables, methods.
Super if present must be the first statement.

5) Difference between method overloading and method overriding in java ?

Important Java interview questions

6) Difference between abstract class and interface ?

7) Why java is platform-independent?

The most unique feature of java is platform-independent.

In any programming language, source code is compiled into executable code. This cannot be run across all platforms.

When javac compiles a java program it generates an executable file called .class file. a class file contains byte codes.

Byte codes are interpreted only by JVM’s. Since these JVM’s are made available across all platforms by Sun Microsystems, we can execute this byte code on any platform.

Byte code generated in a windows environment can also be executed in a Linux environment. This makes the java platform independent.

Join us on Telegram

8) What is method overloading in java?

A class having two or more methods with the same name but with different arguments then we say that those methods are overloaded.

Static polymorphism is achieved in java using method overloading. Method overloading is used when we want the methods to perform similar tasks but with different inputs or values. When an overloaded method is invoked java first checks the method name, the number of arguments, type of arguments; based on this compiler executes this method. The compiler decides which method to call at compile time. By using overloading static polymorphism or static binding can be achieved in java.
Note: Return type is not part of the method signature. we may have methods with different return types but return type alone is not sufficient to call a method in java.

9) What is the JIT compiler?

JIT compiler stands for Just in time compiler. JIT compiler compiles byte code into executable code. JIT is a part of JVM.JIT cannot convert a complete java program into executable code it converts as and when it is needed during execution.

10) What is bytecode in java?

When a javac compiler 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 which are to be executed only by JVM. JVM can understand these byte codes

11) Difference between this() and super() in java ?

this() is used to access one constructor from another within the same class while super() is used to access the superclass constructor. Either this() or super() exists it must be the first statement in the constructor.

12) What is a class?

Classes are the fundamental or basic unit in Object-Oriented Programming . A class is a kind of blueprint or template for objects. Class defines variables, methods. A class tells what type of objects we are creating. For example, taking a Department class tells us we can create department-type objects. We can create any number of department objects. All programming constructs in java reside in class. When JVM starts running it first looks for the class when we compile. Every Java application must have at least one class and one main method. Class starts with the class keyword. A class definition must be saved in a class file that has the same class name. The file name must end with .java extension

public class FirstClass 
{
public static void main(String[] args)
{
System.out.println(“My First class”);
}
}

If we see the above class when we compile JVM loads the FirstClass and generates a .class file(FirstClass.class). When we run the program we are running the class and then executes the main method

13) What is an object ?

It’s basics of java interview questions, An Object is an instance of the class. A class defines the type of object. Each object belongs to some class. Every object contains a state and behavior. The state is determined by the value of attributes and behavior is called
method. Objects are also called an instance. To instantiate the class we declare with the class type

Read More..

--

--