Top 10 Frequently asked Basic Java interview Questions for Freshers

1) How can you call one constructor from other constructor ?

The process of calling one constructor from the other is termed as constructor chaining.

There are 2 ways of constructor chaining.

  • Within same class: this() keyword is used when constructor is within same class
  • From base class: super() keyword is used when constructor is called from base class.

Rules of constructor Chaining:

  • this() keyword should be the first be first line in constructor.
  • There should be At least one constructor without this() keyword.
  • Chaining of constructor can be in any order.

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

Method OverloadingMethod overriding
Method Overloading is a compile time polymorphism.Method Overriding is a run time polymorphism.
It helps to rise readability of the programWhile it is used to grant the specific implementation of the method which is already provided by its parent class or super class.
It occurs within the class.While it is performed in two classes with inheritance relationship.
Method overloading may or may not require inheritanceWhile method overriding always needs inheritance.
In this, methods must have same name and different signature.While in this, methods must have same name and same signature.
In method overloading, return type can or cannot be same, but we must have to change the parameter.While in this, return type must be same or co-variant.

3) Difference between abstract class and interface ?

  Abstract classInterface
 An abstract class can extend only one class or one abstract class at a time An interface can extend any number of interfaces at a time
  An abstract class can extend another concrete (regular) class or abstract class An interface can only extend another interface
 An abstract class can have both abstract and concrete methods An interface can have only abstract methods
 In abstract class keyword “abstract” is mandatory to declare a method as an abstract In an interface keyword “abstract” is optional to declare a method as an abstract
 An abstract class can have protected and public abstract methods An interface can have only have public abstract methods
 An abstract class can have static, final or static final variable with any access specifier. interface can only have public static final (constant) variable

4) Difference between this() and super() keywords in java ?

We have already explained the answer to this question in Que no 1. Both are used for constructor calls. So, manipulate your answers accordingly.

5) What is encapsulation ?

Well, the answer to this question is not one sentence, Cross questions will come so be prepared for cross questions. Answer – Encapsulation is a process of wrapping code and data together into a single unit. For example: A capsule which is mixed with several medicines, you only know the name of the capsule and for what it will be useful.

Advantages of Encapsulation:

  • By Providing only Setter or only Getter you can restrict the class access to read-only or write-only.
  • It provides you control over the data.
  • It is used to achieve data hiding in java because other class will not be able to access the data through the private data members.
  • Encapsulated classes are easy to test so it is better for Unit testing.

6) Why main() method is public, static and void in java ?

In every java program there is one “public static void main” and every word in the line has its own importance and you should know the concept behind it.

Public : Public is a access modifier, which tells that from where and who can access the method. making the main() method as public means that main method can be accessed globally. It is made public so that JVM can access it from any class.

Static : main() method is made static so that JVM can invoke it without instantiating the class(without Object Creation). It saves unnecessary wastage of memory.

void : it is a java keyword which specifies that the method returns nothing. As main() method doesn’t return anything, its return type is made as void. As soon as the main method terminates, the program also terminates, so it doesn’t make any sense to make main() method as void.

main : main is the most important methods of java program. when you run your program, JVM searches for main() method and start its execution from main method. You can say that main() method is the entry point of any program.

7) Difference between ‘>>’ and ‘>>>’ operators in java?

Java supports two type of right shift operator.  The operator ‘>>’ is signed right shift operator while ‘>>>’ is an unsigned right shift operator.

What does that mean? It means that when you use ‘>>’ operator on a negative number, the number will still be negative. Also, signed right shifting by one is equivalent to dividing the number by two, even if the number is negative.

On the other hand, Unsigned shifting ignores the sign of the number, and if you use ‘>>>’ on a negative number will result in a positive number — which is why you shouldn’t use the unsigned shift unless you are considering your number as unsigned.

8) what are static initializer and how to use it in Java ?

Static initializer is static block of code in java class, and run only one time before the constructor or main method is called. usually it is denoted as static{….} anywhere inside a class and executed by virtual machine when class is called.  There are some things to remember about static blocks.  

  • No return statements are supported.
  • this or super keywords are not supported.
  • Argument are not supported.
  • You can use it anywhere in a class.

Here is an example:

package com.example.learnjava;
import java.util.ArrayList;
public class Fruit {
static {
System.out.println("Inside Static Initializer.");

// fruits array
ArrayList<String> fruits = new ArrayList<>();

// print fruits
for (String fruit : fruits) {
System.out.println("End Static Initializer.\n");
public static void main(String[] args) {
System.out.println("Inside Main Method.");

Inside Static Initializer.
End Static Initializer.
Inside Main Method.

9) What is JIT compiler ?

Just-In-Time (JIT) Compiler is one of the very important component of JRE. It is responseble to perform optimization of java code at runtime.

JIT compilers and JVM interacts with each other in order to improve performance at run time and compile suitable bytecode sequences into native machine code. JIT compile is more capable in executing the native code as compared to JVM which interprets the same sequence of bytecode repeatedly.  

10) Why java is platform independent?

Java is called platform independent because java compiler generates a code which can be run on any platforms provided it has a JVM(Java Virtual Machine) installed. Other languages like C, C++ , which generated a .exe file which can only be run at OS (Operating system) level. You can also run a java program on a android or apple mobile provided it has a JVM installed on it.

Leave a Reply