Java Interview Q & A

Top 30 java programming interview questions and answers.

1.What is java ?

Java is an object-oriented ,high level and platform independent programming language.

Java is a secure,robust and multi-threaded etc programming language.

Java was developed by James Gosling in 1991

There are the lots of websites and games are developed using the java.

2.why java is not fully object-oriented programming language ?

Java is not fully object-oriented programming language because it support primitive data types such as byte,boolean,char,int, float,double etc.which are not objects so java is not fully object-oriented programming language.

3.Why java is platform independent ?

Java is a platform independent because it works on the “WORA” concept means “write once run anywhere”.

that means Compiled java codes(byte codes) can run on the different operating systems like Window,Linux and Mac etc

4.What is JVM in java ?

JVM stands for Java Virtual Machine.

It provides the run time environment in which java compiled codes(byte codes) can be converted into the executable codes(machine codes).

5.Which class is the super class of all the classes ?

‘Java.lang.object’  is the super class of the all classes.It does not need to extend.

6.Write simple program in Java to print “First Java Program”.

Public class FirstJavaProgram

{
Public static void main(String[] arg)

{
System.out.println(“First Java Program”);

}

}

Output:

First Java Program

7.What is the output of the java program ?

Public class Test

{
Public static void main(String[] arg)

{
System.out.println(10+20+”placementpreparation”);

}

}

Output:

30placementpreparation

Hint:

Here  first 10 and 20 will be added ie 30 and treated as a string and then concatenate with the string ‘placementpreparation” and produce 30placementpreparation.

8.What is the difference between object-oriented programming language and object based programming language ?

There are the following differences between the object-oriented and object-based programming language.

1.Object-oriented programming languages follow all the features of Oops whereas Object-based programming langauge does not follow all the features of Oops like inheritance and polymorphism.

2.Example of Object-oriented programming languages are Java,C#,Smalltalk,Kotlin etc

3.Example of Object-based programming languages are JavaScript,VBScript etc.

9.What are the java IDE’s ?

NetBeans and Eclipse are the IDE’s of java programming language.

10.What do you mean by Local and Instance variable in Java ?

Local variable:

When variable is decleared inside the method then it known as local variable.

It’s scope is valid only inside the method.

Instance variable:

When variable is decleared inside the class but outside the method then it is known as instance variable.

It’s scope is valid inside the class.

Example to understand the concept of variables in java:

Public class TestVariables

{
Public static void main(String[] arg)

{
int sum=10; //This is instance variable

void methodsuman( )

{

int multiply=20;// This is local variable

}

}

}

11.What is Object ?

An object is an entity which contains the ‘state’ and ‘behavior’.

Example: chair,pen,table etc

Here ‘state’ represents the fields ie variables.

Here ‘behavior’ represents the methods ie where we write the business logic

Object is created using the ‘new’ keyword in java.

Example:

public class Multiply //class name declaration

{

public static void main(String[] arg)

{
Multiply mul=new Multiply( );//Creating the object of Multiply class

}

}

12 What is Class ?

Class is a collection of objects.It contains variables and methods.

All the java codes are written inside the class.

Variables are used to define the state of the class.

Methods are the place where business logics are written.

Example:

public class Multiply{ //Here is class declaration

int a=5,b=5; //This is variables

public void multi( ) //This is method declaration

{

int c=a*b;

}}

13.Explain thepublic static void main(String[ ] arg)”.

1.public->Here ‘public‘ is a access modifier which defines the scope of the method.Here public means we can access this method outside the class also.

2.static->Here ‘static‘ is a keyword.With the help of ‘static’ we can access the method without creating the object.

3.Void->Here ‘void’ is the return type of this method.Void indicates that method will not return any value.

4.main->Here ‘main‘ is the entry point of program which is specify by the JVM

5.String[] arg->This is string type parameter which is passed to the main method.

14 What are the Constructors in Java ?

Constructor is a special type of method which is used to initialize the object state.

Constructor is called when class object is created and memory is allocated for the object.

Rules for Constructor in Java

1.Constructor name must be same as its class name.

2.Constructor does not contain any return type like void,int,float etc.

Types of Constructors:

There are the two types of Constructors:

1.Default constructor(with no argument)

2.parameterized(with argument)

1.Default Constructor

When constructor does not contain argument or parameter then it is called default constructor.

Example:

Public class FirstConstructorProgram //FirstConstructorProgram is the class name

{

Int y;

Public FirstConstructorProgram( ) //FirstConstructorProgram is the constructor name(same as class name)

{

y=20; // Here initialize the value of y
}

Public static void main(String[ ] arg)

{

FirstConstructorProgram obj=new FirstConstructorProgram ( ); //creating the object of class for calling the constructor

System.out.println(obj.y);

}

}

Output:

20

2.parameterized(with argument) Constructor

When constructor contains the argument or parameter then it is known as parameterized  Constructor.

Argument or parameter may be one,two,three or etc.

Argument or parameter may be integer ,string etc

Example

Public class FirstParameterizedConstructorProgram

{
Int y;

Publuc FirstParameterizedConstructorProgram(int sum)

{

sum=y;
}

Public static void main(String[ ] arg)

{

FirstParameterizedConstructorProgram obj1=new FirstParameterizedConstructorProgram(5);

System.out.println(obj1.y);

}

}

Output:

5

15 What is Method overloading ?

When the class contains two or more methods with the same name but different parameters is known as Method overloading.

It saves the time and more readability.

It is the example of static polymorphism.

There are different ways to overload the methods.

1 By changing the sequence of data type of parameter

Example:

add(int a ,char b)

add(char b,int a)

2. By Changing the number of data type of parameter

Example:

sum(int a, int b)

sum(int a,int b ,int c)

3 By Differentiate the data type of parameter

Multiply(int a)

Multiply(char a)

16 What is Method Overriding ?

When the sub-class contains the same method ,same parameter and same return type as a method in its parent or super class then it is known as Method overriding.

It is the example of run-time polymorphism

It must be a IS-A relationship.(ie inheritance)

Exaple:

//Creating the parent class

Public Class ParentClass

{
Void sum( ) //ParentClass method

{
System.out.println(“Hello suman”);

}

}

//Creating the child class

Public class ChildClass extends ParentClass

{

Void sum( ) //ChildClass method same as parent class

{

System.out.println(“Hii suman”);

}

Public static void main(String[ ] arg)

{

Child  obj=new Child( );

obj.sum( );

}

}

Output

Hii suman

Note: “extends” keyword is used for inheriting the features of parent or existing class class and also adding the features in the child or sub class.

17 What is Abstract Class in java ?

When the class is declared with the ‘abstract’  keyword then it is known as abstract class.

It contains abstract methods(means without the body) and non-abstract(with the body) methods

Before Knowing the “Abstract class in java”.First know what is “Abstraction”.

Example:

Public abstract class Abc //This is abstract class
{
 void abstract sum( ); 
}

Hints:

Here  Public void abstract sum( );  is a abstract method.This method can’t be  implement in the ‘Abc’ class.This method will be implement in another class by extending the ‘Abc’ class.

Complete Implementation of Abstract method “sum”.

public class Bcd extends Abc
{
void sum()
{
System.out.println("Hello India");
}
public static void main(String[] arg)
{
Abc obj=new Bcd();
obj.sum();
}
}
Output:
Hello India

18 What is java collections framework in java ?

Java collections is a collection of classes and interfaces which is used to store multiple objects/values into single unit.

It provides the well structured to store group of objects.

It provides the different type of classes and interfaces.

Classes Example:ArrayList,LinkedList,Vector etc

Interfaces Example:List,set and queue etc

Note:Collections is a class and Collection is a interface

19 What do you mean by List,Set and Queue ?

List: List extends the Collection interface.It is an ordered list of objects.It contains the duplicate or same element.

Declaration:

Public interface List<E> extends Collection<E>

Here :<E> represents the interface is generic type.

Example:

Input:[A,A,B,C,D,D

Output:[A,A,B,C,D,D]

Set: Set extends the Collection interface.It is an unordered list of objects.It does not contain duplicate or same element.

Declaration:

Public interface Set<E> extends Collection<E>

Example:

Input:[A,A,B,C,D,D]

Output:[A,B,C,D]

Queue :Queue extends the Collection interface.It works on the concept of FIFO means First In  First Out.

Declaration:

Public interface Queue<E> extends Collection<E>

Example:

Input:[A,B,C]

Output:[A,B,C]

20 What are the differences between the “Iterator” and “ListIterator” ?

Iterator:

It is used to traverse the element in forward direction only.

It can be used in List,Map,Set etc.

It can not add the element in the collection.

It is unable to modify the element in the collection.

ListIterator:

It is used to traverse the element in both forward and backward direction.

It can be used in List only.

It can add the element in the collection

It is able to modify the element in the collection.

21 What is Exception in java ?

Exception is an abnormal condition which prevents  the normal flow of execution of the program.

It occur during the execution of the programs

Some ‘Exception’ examples are given below IOException,FileNotFoundException,ArrayIndexOutOfBoundsException etc

22 What is Exception handling in java?

Exception handling is process to handle the run-time error such as ClassNotFoundException,SQLException etc.

It keep maintain the normal flow of the program

Types of Exception

1.Checked Exception

2.Unchecked Exception

1.Checked Exception

When the errors are occured at the comiple- time then it is known as Checked Exeception.

Example: SQLException,IOException etc.

2.Unchecked Exception

When the errors are occured at the run-time then it is known as Unchecked Exception.

Example:ArithmeticException,NullPointerException etc

Exception handling Example using try and catch:

try
{
//code block where risk may occur
}
catch(Exception)
{
//Exceptions are caught in catch block
}

23 What is Thread in java ?

A thread is a light weight sub-process

A thread class is defined in java.lang package

Every java program has at least one thread which is created by JVM called main thread

Thread can be created by extending the Thread class and implementing the Runnable interface.

Thread execute independently

Example:

public static void main(String[] arg)//main thread start from here
{
}

24 How can you create Thread in java ?

There are two ways in which we can create Thread in java.

1.Extending the Thread class

public class Abc extends Thread
{
public void run()
{
System.out.println("Thread program by extending thread class");
}
public static void main(String[] arg)
{
Abc obj=new Abc();
obj.start();
}
}
Output:
Thread program by extending thread class

2.Implementing the Runnable interface

public class Bcd implements Runnable
{
public void run()
{
System.out.println("Thread program by runnable interface");
}
public static void main(String[] arg)
{
Bcd obj1=new Bcd();
Thread obj2=new Thread(obj1);
obj2.start();
}
}
Output:
Thread program by runnable interface

25 What is String in java ?

Simply string means sequence of character,But in java

String is an object which represents the sequence of character value.

In java,String is immutable means it cannot be changed.

Example:

char str[]={"a","b","c","d"};
String st1=new String(str);

This same as 
String st="abcd";

Java string provides the various methods such as concate,append,equals,split,lenght etc.

26 What is the difference between the StringBuffer and StringBuilder ?

StringBuffer:

StringBuffer is mutable means once object created its value can be chnaged.

The object created by the StringBuffer is stored in heap.

It is thread safe.

StringBuilder:

StringBuilder is same as StringBuffer.

It is not thread safe.

It is more more efficient.

27 Write a program to add two number using command line argument.

public class Abc//Here Abc is the class name
{
public static void main(String[] arg)
{
int sum;
int a=Integer.ParseInt(arg[0]);
int b=Integer.ParseInt(arg[1]);
sum=a+b;
System.out.println (sum);
}
}
Output
Compile:javac Abc.java 2 4  //Enter the value while compiling 
Run:java Abc

sum=6

28 What do you mean by “Interface” in java ?

Multiple inheritance does not support in java.But we can achieve with the help of interface.

Interface is the blueprint of class.It contains fully abstract method and static constant.

It is declared with “interface” keyword.It contains fully abstraction method means without the body and all the fiels are public,static and final by default.

Example:

interface Abc
{
void sum();
}
class Bcd implements Abc
{
public sum()
{
System.out.println("Hello india");
}
public static void main(String[] arg)
{
Bcd obj=new Bcd();
obj.sum();
}
}
Output:
Hello india

29 What is the difference between the “Abstract class” and “Interface” ?

Abstract classInterface
1.It contains both abstract and non-abstract method.1.It contains only abstract method.In java 8,it has default and static method.
2.It does not support multiple inheritance2.It support multiple inheritance
3.Abstract keyword is used to declare abstract class3.Interface keyword is used to declare the interface.
4.We can not achieve fully abstraction.4.We can achieve fully abstraction with help of interface
5.It is extended by the “extends”keyword.5.It is implemented by the implementing “implements” keyword.

30 What is Wrapper class in java ?

It provides the mechanism to convert primitive into object and object into primitive

Primitive TypeWrapper Class
charcharacter
booleanBoolean
byteByte
intInteger