Java Interview Questions: 55+ Most important and most asked

Basics of JAVA based Interview Questions

Q1). What do you understand by Class in Java?

Ans: A class is a blueprint from which objects are created. WE can also define it as it is a group of objects which have common properties. This properties is set by class for all method and properties of class.

Class declaration have component mentioned below:

  • Class name
  • Modifiers
  • Interface
  • Super Clas

Class can Contains

  • Methods
  • Members
  • Constructor
  • Blocks
  • Nested Class
  • Nested Interface

Declaration of Class 

class {
field;
method;
}

 

Q2 ). What do you understand by Object?

Ans : Object is a basic unit of OOPs. It is also an instance of a class. Everything in OOPs is represented using Objects.
In General we can define Object as it is a real world entity that has its own some states and behaviour.

For Example: Cat is a suitable example of an objects. Cat has its own states and behaviour and its is also a real world entity.
Cat can walk, eat is its behaviour and Cat can be white, black is its states.

Objects have some characteristics :

1). State : State Reflects the properties of Objects. Just like Cat can be white, black or may be combination of both is its properties. State can be denoted by attributes.
2). Behaviour : Behaviours shows that how object is responding to other objects. Behaviour can be denoted by Methods.
3). Identity: Every objects should have unique identity. Like if we say cat then it may be a group of cat but If we talk about a particular cat then we need a identity for that particular cat.

Q3). Explain JDK, JVM and JRE.

JDK: JDK stands for Java Development Kit which contains JRE. It gives a development environment for Java applications where developer can code and run java programs.

JRE: JRE Stands for Java Runtime Environment that contains class libraries, loader class and JVM. Its main function is to run Java Program. If you don’t want to develop program then you don’t need JDK But, for only the purpose of execution of java program you only need JRE. JRE comes with JDK bundle you don’t need to download it separately.

JVM: JVM stands for Java Virtual Machine that provide the runtime environment to java code where Java code is executed. JVM is a part of JRE that converts java byte code into machine code. In java, compiler produces code for JVM.

 

Q4). Explain Public Static void Main(String args[]) in Java.

Ans: It is an entry point of any Java program from where Execution starts.
Public: Public is a access modifier that define the accessibility of method. If any method have Public access modifier it means that it can access by any other class.
Static: static is a keyword in java that define class level member. It means that without creating any instance it can be accessible using class name.
void: void is also a keyword that define the return type of any methods. If method have void keyword it means that method will not return any value.
Main: It is a method from where execution starts. It is searched by JVM to start the execution.
String args[]: it is a main method argument.

 

Q5). What is the different between Final, finally and finilize?

Ans: Final:
final is a modifier which is used with classes, methods and variables. If the class is declared as final then that class is not extended by other class. If the method is declared as final then we cannot override that method in the child class. i.e. final method is not overridden by child class. If the variable is declared as final then it behave as a constant and we cannot re-assingment for that variable.

finally:
finally is a block which is always associated with try-catch block.finally block is always executed whether exception is handled or not.finally block is used to execute important code such as closing connection, stream when any exception arise.

finalize();
finalize() is a method which is always invoked by garbage collector.The purpose of finalize() is to perform cleanup operation just before destroying an object.

Q6). What is the difference between “==” and “equals()” method.

Ans: “==” is used for address/reference comparison.
Example:

String str1=new String("prakash");
String str2=new String("prakash");
System.out.println(str1==str2);

output: false

equals() is used for content comparision.

Example:

String str1=new String("prakash");
String Str2=new String("prakash");
System.out.println(str1.equals(str2));

output: true

Q7). Why Java is called as platform independent ?

Ans: After the compilation of Java code, code is converted into byte code. This byte code can be executed in any other system and environment. Because of that it is known as platform independent.

Q8). What is meant by Local variable and Instance variable?

Ans:

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

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

Oops Concept Interview Questions

Q1). Define Constructor.

Ans: We can define as constructor is a method or block which is use to initialise the objects. Constructor should have same name as a name of class which have no return type. It is automatically called by java at the time of object creation.


There are 3 types of constructor
1). Default constructor: It is automatically inserted by Java when we not declared any constructor. It is not visible in your source code.
2). Parameterised constructor : Constructor with arguments are known as Parameterised constructor.
3). No-arg Constructor : it is very similar to default constructor. We declare it when we want to perform some operation at a time of object creation then we write that code in no-args constructor. For example some initialization of value.

 

Q2). Explain access modifiers in Java.

Ans: Access modifiers is a keywords in Java that define the accessibility of any class, methods and member variables.


There are 4 types of access modifiers in Java:-

  • Public
  • Private
  • Protected
  • Default

 

ModifierDefaultPrivateProtectedPublic
Same classYESYESYESYES
Same Package subclassYESNOYESYES
Same Package non-subclassYESNOYESYES
Different package subclassNONOYESYES
Different package non-subclassNONONOYES
 
Q3). Explain some features of oops.

Ans:
Inheritance: Inheritance is process in which child objects of child class acquires the all properties and behaviour of its parent class. It improves the code reusability.
Abstraction: Abstraction means hide the complexity and show the functionality. For examples in education portal you just sign up and then login by giving credential and then access your dashboard but you won’t aware of how its happening in backend. It is known as abstraction.
Encapsulation: Encapsulation is a process of binding the data and methods together in a single entity known as class for data safety.
Polymorphism: Polymorphism defines to perform single actions in multiple form. It is derived from two word Poly + morphs that means many forms.
It is of two types:

  • Method overloading
  • Method Overriding
 
Q4). What is this and super keyword in Java?

Ans: 
this : this is a keyword is use to access the member of present class.
super : super keyword is used to access the member of parent class.

 

Q5). What is the different between static and non-static method of java?

Ans: 
Static method: static method is a class level method. We need not required any instance to access them we can access it directly using class. A static method can only access static variable.
non-static method: non-static method belongs to objects of the class. It can be accessible inside the static method with the help of class instance.

 

Q6). What do you mean by Polymorphism?

Polymorphism is derived from two greek word “poly” and “morphs” means many form. It allows to perform single task in multiple form. In Java term we can say that there can be multiple methods with same name but different functionality.


It is of two types:
1). Compile time polymorphism
2). Runtime Polymorphism

Compile time polymorphism is also known as method overloading and Runtime polymorphism is known as overriding.

 

Q7). Explain Method overloading and method overriding.

Ans:

Method overloading: In method overloading a class have different methods with same name but different number of arguments. In Overloading case methods should have different signature. It is also known as compile time polymorphism.

Method Overriding: In Method Overriding sub class have similar method as parent class. Similar mean name, return type, parameter of methods for both parent and sub class method.

 

Q8). How many ways you can overload method?

Ans: There are various way to overload a method :

1) By number of passing parameters the valid case for overloading the method is
add(int,int)
add(int,int,int)

2) By changing the data type of parameter
add(int,int)
add(float,int)

3) By changing the sequence of data type
add(int, float)
add(float,int)

 

Q9). What is the some invalid cases of method overloading in java?

Ans: In java if two methods having the same name, same parameters but different return type, then this is not a valid method overloading. Also if two methods having the same name, same parameters and different return type, then this is also not a valid method overloading. This will throw compilation error.

Example:
int sum(int,int)
float sum(int,int)

 

Q10). What do you mean by abstract class?

Ans: Abstract class is declared with the help of Abstract Keyword. It can have both abstract and non-abstract method. We cannot instantiate abstract method. It is extended by another class which can implement its methods to use. It can have static and final methods.

 

Q11). What do you mean by Interface?

Ans: Interface is a blueprint of class that have abstract and public methods without body. Class that implements interface should implement all methods of interface.

public interface Man {
public void eat();
public void sleep();
public void walk();
}

Java String Interview Questions

Q1). Difference between String and StringBuffer.

Ans:

String: String object is immutable but the StringBuffer objects are mutable. Immutable means once we create a String object we cannot perform any changes on object.

Example of String:

String str=new String("prakash");
str.concat("kumar");
System.out.println(str);

output:- prakash

 

Example of StringBuffer:

StringBuffer strbfr=new StringBuffer("prakash");
strbfr.append("kumar");
System.out.println(strbfr);

output:- prakash kumar


Q2). Difference between StringBuffer and StringBuilder.


Ans: Both StringBuffer and StringBuilder are mutable.

StringBuffer:
All method of StringBuffer is synchronized. Its performance is low because every task is dependent to each other.

StringBuilder:
Methods are not synchronized. Its performance is high because more than one thread can allowed.

 

Q3). Difference between String, StringBuilder, and StringBuffer.

Factor

String

StringBuilder

StringBuffer

Storage Area

Constant String Pool

Heap Area

Heap Area

Mutability

Immutable

Mutable

Mutable

Thread Safety

Yes

No

Yes

Performance

Fast

More efficient

Less efficient

 

 

Java Array & Collection Interview Questions

Q1). Difference between Array and Array List

Ans:

Array

  • Array is fixed in size which is decided at a time of array declaration.
  • Array can contain both primitives data and objects.

ArrayList

  • Size of ArrayList is dynamic.
  • It is a part of collection framework of java.
  • ArrayList can contain only object. After Java 5 It converts primitive data into object itself.

Q2). What do you mean by Collections in Java.

Ans: Collection is a Java framework that have some predefined class and interfaces to store and manipulate the group of objects.

Interfaces comes with Collection framework:

  • Set
  • List
  • Queue
  • Dequeue

Classes comes with Collection framework:

  • ArrayList
  • LinkedList
  • HashSet
  • TreeSet
  • LinkedHashSet etc.

Q3). Explain the lists that are present in Java Collection.

Ans:

Types of Lists are:

1). ArrayList

  • Fast iteration and fast Random Access
  • It is an ordered collection.
  • It is not sorted collection.

Example:

public class Animal{
public static void main (String [ ] args){
ArrayList <String>names=new ArrayList <String>();
names.add ("Dog");
names.add ("Cat");
names.add ("Donkey");
names.add ("Cow");
System.out.println (names);
}
} 

Output:

[Dog, Cat, Donkey, Cow]

 

2). Linked List

  • Performance is slow than ArrayList.
  • Maintain the Insertion Order.
  • It can have duplicate values
public class Animal{
public static void main (String [ ] args){
Linkedlist <String>names=new Linkedlist <String>();
names.add ("Dog");
names.add ("Cat");
names.add ("Donkey");
names.add ("Cow");
System.out.println (names);
}
} 

Output:

[Dog, Cat, Donkey, Cow]

 

3). Vector

  • Similar as ArrayList.
  • Maintain the Insertion Order.
  • It can have duplicate values.
  • Its Methods are Synchronized.
public class Animal{
public static void main (String [ ] args){
Linkedlist <String>names=new Linkedlist <String>();
names.add ("Dog");
names.add ("Cat");
names.add ("Donkey");
names.add ("Cow");
System.out.println (names);
}
} 

Output:

[Dog, Cat, Donkey, Cow]

Q4). Explain the Set and their types in a Java Collection.

Ans: Set cares of uniqueness, It means that it does not allow duplicate value.

 

Types of Set in Java Collection are:

1). Hash Set

  • HashSet is unordered and unsorted.
  • It uses the hash code of the object to insert the values.
  • We can use Hash Set when order is not concerned and we want no duplicate value.

Example:

public class Animal{
public static void main (String [ ] args){
HashSet <String>names=new HashSet <String>();
names.add ("Dog");
names.add ("Cat");
names.add ("Donkey");
names.add ("Cow");
names.add ("Dog");
System.out.println (names);
}
}  

Output:

[Dog, Cat, Donkey, Cow]

 

2). Linked Hash set

  • It maintains the insertion order.
  • Does not allows duplicate value.
  • It can be used when iteration order is required.
public class Animal{
public static void main (String [ ] args){
LinkedHashSet <String>names=new LinkedHashSet <String>();
names.add ("Dog");
names.add ("Cat");
names.add ("Donkey");
names.add ("Cow");
names.add ("Dog");
System.out.println (names);
}
}  

Output:

[Dog, Cat, Donkey, Cow]

 

3). Tree Set

  • It sorts the elements in ascending order.
  • It does not allow duplicate values.
public class Animal{
public static void main (String [ ] args){
ArrayList <String>names=new ArrayList <String>();
names.add ("Dog");
names.add ("Cat");
names.add ("Donkey");
names.add ("Cow");
names.add ("Dog");
System.out.println (names);
}
}  

Output:

[Dog, Cat, Donkey, Cow]

 

Q5). Difference between HashMap and HashTable

Ans:

HashMapHashTable
1. HashMap is non synchronized1. HashTable is synchronized
2. HashMap allows NULL Key and Value.2. HashTable does not allows any NULL Key and Value.
3. Performance of HashMap is fast.3. HashTable is slow.
4. HashMap inherits class AbstractMap.4. HashTable inherits class Dictionary.
5. We can traverse HashMap using Iterator.5. We can traverse HashTable using Iterator and Enumerator.

Q6). Difference between HashSet and TreeSet.

Ans:
HashSet TreeSet
1. HashSet is faster and gives better performace. 1. TreeSet is little bit slower than HashSet.
2. HashSet performs operation in constant time 2. TreeSet performs operation in Log(n) time.
3. HashSet does not follow any order 3. TreeSet elements are arranged in ascending order.
4. HashSet does not hold duplicate value. 4. TreeSet also does not hold duplicate value.