Home Interview Questions Top 50+ Java Interview Questions with Answers

Top 50+ Java Interview Questions with Answers

by Sikander Iqbal
java interview questions

We covered nearly 50 + primary Java interview questions in this tutorial for fresh and experienced candidates. 

Table of Contents

Q.1- What is Java? 

Java is a high-level programming language and is platform-independent. Java is a collection of objects. It was developed by Aun Microsystems. There are a lot of applications, websites and games that are developed using Java. 

Q.2- What are features of Java? 

The following are the features of Java: 

OOP Concepts:

  • Object-Oriented 
  • Inheritance 
  • Encapsulation 
  • Polymorphism 
  • Abstraction 

Other Concepts:

  • Platform independent: A single program works on different platforms without any modification. 
  • High Performance: JIT (Just In Time compiler) enables high performance in Java. JIT converts the bytecode into machine language and then JVM starts the execution. 
  • Multi-threaded: A flow of execution is known as a Thread. JVM creates a thread which is called main thread. The user can create multiple threads by extending the thread class or by implementing Runnable interface. 

Q.3- How does Java enable high performance? 

To enable high-performance, Java uses just in time compilers. JIT is used for byte coding of the instructions. 

Q.4- What are the Java IDEs? 

Eclipse and NetBeans are the IDEs of Java. 

Q.5- What do you mean by Constructor? 

The following points will explain the Constructor in detail: 

  • A constructor is called up according to the class when a new object is installed on a program. 
  • The builder is a process with the same name as the name of the unit. 
  • A default builder will be created if a user does not construct a builder implicitly. 
  • You can overload the manufacturer. 
  • Once a builder with a parameter is created, the user will specifically create a builder with no parameter. 

Q.6- What is meant by the Local variable and Instance variable? 

Local variables are defined in the method and scope of variables that have existed inside the method itself. 

An instance variable is defined inside the class and outside the method and scope of variables exist throughout the class.   

Q.7- What is a class? 

The class is the definition of all Java codes. A class has different parameters and methods. 

Variables are attributes that define the class status. 

Methods is the exact location for carrying out business logic. It contains a set of declarations (or) to fulfil the specific requirement. 

Q.8- What is an Object? 

An instance of a class is called object. The object has state and behaviour. 

Whenever the JVM reads the “new()” keyword then it will create an instance of that class. 

Q.9- What are the OOP concepts? 

The OOP concepts include: 

  • Inheritance 
  • Encapsulation 
  • Polymorphism 
  • Abstraction 
  • Interface 

Q.10- What is Inheritance? 

Inheritance means one class can extend to another class. So that the codes can be reused from one class to another class. 

Existing class is known as Super class whereas the derived class is known as a sub class. 

Example: 

Super class:

public class Manipulation{
}

Sub class:

public class Addition extends Manipulation{
}

Inheritance is applicable for public and protected members only. Private members can’t be inherited. 

Q.11- What is Encapsulation? 

Purpose of Encapsulation: 

  • Protects the code from others. 
  • Code maintainability. 

Example: 

We are declaring ‘a’ as an integer variable and it should not be negative. 

public class{
    int a=5; 
}

If someone changes the exact variable as “a = -5” then it is bad. 

In order to overcome the problem, we need to follow the below steps: 

  • We can make the variable as private or protected one. 
  • Use public accessor methods such as set<property> and get<property>

So that the above code can be modified as: 

public class Addition{
    private int a = 5; //Here the variable is marked as private
}  

Below code shows the getter and setter. 

Conditions can be provided while setting the variable. 

get A{
} 
set A(int a){ 
    if(a>0){
    // Here condition is applied 
    }
}

We need to render all the instance variables as private for encapsulation and build setter and getter for those variables. Which in effect will cause others to call the setters instead of directly accessing the data. 

Q.12- What is Polymorphism? 

Polymorphism means many forms. 

A single object can refer to the super class or sub-class depending on the reference type which is called polymorphism. 

Example: 

public class Manipulation{ 
    //Super class 
    public void add(){ 
    }
}
public class Addition extends Manipulation(){ 
    // Sub class 
    public void add(){ 
    }
    public static void main(String args[]){ 
        
        //Manipulation is reference type and Addition is reference type 
        Manipulation addition = new Addition();
        addition.add(); 
    }
}

Using Manipulation reference type we can call the Addition class “add()” method. This ability is known as Polymorphism. 

Polymorphism is applicable for overriding and not for overloading

Q.13- What is meant by Method Overriding? 

Method overriding happens if the sub class method satisfies the below conditions with the Super class method:

  • Method name should be same 
  • Argument should be same 
  • Return type also should be same 

The key benefit of overriding is that the Sub class can provide some specific information about that sub class type than the super class. 

Example: 

public class Manipulation{
    //Super class
    public void add(){
    }
}
public class Addition extends Manipulation{
    public void add(){
    }
    public static void main(String args[]){
        Manipulation addition = new Addition(); 
        //Polymorphism is applied
        addition.add(); 
        // It calls the Sub class add() method
    }
} 

addition.add() method calls the add() method in the Sub class and not the parent class. So it overrides the Super class method and is known as Method Overriding. 

Q.14- What is Method Overloading? 

Method overloading happens for different classes or within the same class. 

For method overloading, subclass method should satisfy the below conditions with the Super class method (or) methods in the same class itself: 

  • Same method name 
  • Different argument type 
  • May have different return types 

Example: 

public class Manipulation{ 
    //Super class
    public void add(String name){ 
        //String parameter
    }
}
public class Addition extends Manipulation(){
    public void add(){
        //No Parameter
    }
    public void add(int a){ 
        //integer parameter
    }
    public static void main(String args[]){
        Addition addition = new Addition(); 
        addition.add(); 
    }
} 

Here the add() method having different parameters in the Addition class is overloaded in the same class as well as with the super class. 

Note: Polymorphism is not applicable for method overloading. 

Q.15- What is Interface in Java? 

Multiple inheritance cannot be achieved in java. To overcome this problem Interface concept is introduced. 

An interface is a template which has only method declarations and not the method implementation. 

Example: 

public abstract interface IManupulation{ 
    //Interface declaration 
    public abstract void add(); //method declaration 
    public abstract void subtract(); 
} 
  • All the methods in the interface are internally public abstract void
  • All the variables in the interface are internally public static final that is constants. 
  • Classes can implement the interface and not extends. 
  • The class which implements the interface should provide an implementation for all the methods declared in the interface. 
public class Manupulation implements IManupulation{ 
    //Manupulation class uses the interface 
    public void add(){ 
    
    }
    public void subtract(){
    
    }
}

Q.16- What is Abstract class in Java? 

We can create the Abstract class by using “Abstract” keyword before the class name. An abstract class can have both “Abstract” methods and “Non-abstract” methods that are a concrete class. 

Abstract method: 

The method which has only the declaration and not the implementation is called the abstract method and it has the keyword called “abstract”. Declarations are the ends with a semicolon. 

Example: 

public abstract class Manupulation{ 
    public abstract void add(); //Abstract method declaration 
    public void subtract(){ 
        
    }
}
  • An abstract class may have a Non- abstract method also. 
  • The concrete Subclass which extends the Abstract class should provide the implementation for abstract methods. 

Q.17- Difference between Array and Array List. 

The Difference between Array and Array List can be understood from the below table: 

                        Array                                       
 
 
   Array List     
Size should be given at the time of array declaration. 
 
String[] name = new String[2] 
Size may not be required. It changes the size dynamically. 
 
ArrayList name = new ArrayList 
To put an object into array we need to specify the index. 
 
name[1] = “book” 
No index required. 
 
name.add(“book”) 
Array is not type parameterized ArrayList in java 5.0 are parameterized. 
 
Eg: This angle bracket is a type parameter which means a list of String. 

Q.18- Difference between String, String Builder, and String Buffer. 

String: String variables are stored in “constant string pool”. Once the string reference changes the old value that exists in the “constant string pool”, it cannot be erased. 

Example: 

String name = “book”; 

Constant string pool 

Constant string pool

If the name value has changed from “book” to “pen”. 

Constant string pool 

Constant string pools

Then the older value retains in the constant string pool. 

String Buffer: 

  • Here string values are stored in a stack. If the values are changed then the new value replaces the older value. 
  • The string buffer is synchronized which is thread-safe. 
  • Performance is slower than the String Builder. 

Example: 

String Buffer name =”book”; 

Stack

Once the name value has been changed to “pen” then the “book” is erased in the stack. 

Stack1

String Builder: 

This is same as String Buffer except for the String Builder which is not threaded safety that is not synchronized. So obviously performance is fast. 

Q.19- Explain about Public and Private access specifiers. 

Methods and instance variables are known as members. 

Public: 

Public members are visible in the same package as well as the outside package that is for other packages. 

Public

Public members in Class A are visible to Class B (Same package) as well as Class C (Different package). 

Private: 

Private members are visible in the same class only and not for the other classes in the same package as well as classes in the outside packages. 

Private

Private members in class A is visible only in that class. It is invisible for class  B as well as class C. 

Q.20- Difference between Default and Protected access specifiers.