Top 20 Latest Java interview questions for 2020-part I

Hi buddy ,In this tutorial we will discuss the top 20 java interview questions answers,These questions will help you to crack the java interviews for both fresher and experienced developers.

So Let’s start,without wasting time

1.Why is Java platform independent ?

Java is platform independent because execution of code does not depend upon the configuration of Operating system.

2.What is a constructor ?

An constructor is a special type of method which is used to initialise the state of an object

A constructor is involved when an object is instantiated i.e. when we use a new keyword.

Rules of constructor

1.Constructor name must be same of class name

2.Constructor does not have return type

3.Constructor can not be abstract,find , static & Synchronised.

Example:

public class Test
{
Test( )   //This is constructor
{
System.out.print(“Hello placement preparation”);
}

public  static void main(String[ ] args)
{
Test obj=new Test( );//calling constructor 
}
}

Output:

Hello placement preparation

3.What are the  types of Constructors ?

There are two types of constructors

1.Default constructor

2. Parameterised constructor

1.Default Constructor

When a constructor does not contain any parameter then it is known as default constructor.

If we do not define any constructor  then compiler create default constructor(without arguments) for the class

It provides the default value 0 ,null to the corresponding integer & string.


public class Test
{
int age;
String name;

Test( )
{
System.out.print(“This is example of default constructor”);
}

public static void main(String[ ] args)
{
Test test=new Test( );//Here is calling constructor

System.out.println(“Name=:”,+test.name);
System.out.print(“Age=:”,+test.age);
}
}

Output

This is example of default constructor

Name=:null

Age=:0

2.Parameterised constructor

When a constructor contains  parameters then it is known as default constructor.

It is used to initialise the data member of class with the different values.

public class Test
{
int age;
String name;

Test(String name,int age)
{
this.name=name;
this,age=age;
}

public static void main(String[ ] args)
{
Test obj=new Test(“kumar”,24 );//Here calling the constructor

System.out.println(“Name=:”,+obj.name);
System.out.print(“Age=:”,+obj.age);
}
}

Output

Name=:kumar

Age=:24

4.What is a static keyword in java ?

Static is a keyword which is used to share the common property for all the objects inside the class.

It is mainly used for memory management i.e It takes memory only once at the time of class loading.

The main benefit of static keywords is that we do not  need to create objects for calling the method.

Example:

public class TestExample
{
 static void  message( )
{
System.out.println(“This is the example of static method”);
}

public static void main(String[ ] args)
{
 message( );
}
}

Output

This is the example of static method

5.What are wrapper classes in java ?

Wrapper classes converts the primitive data types into objects & Vice-versa(Objects to corresponding primitive data types)

With the help of wrapper classes ,we can write code in a fully object-oriented form.

Example:

Primitive Data type                                  Wrapper Class

int                                                                  Integer

char                                                               Character

boolean                                                         Boolean   

byte                                                               Byte

Wrapper class provides the two features:

1.AutoBoxing

2.UnBoxing

1.AutoBoxing

AutoBoxing converts the primitive data types to the objects to their corresponding wrapper classes.

Example:

int to Integer,char to Character etc

2.UnBoxing

It is just the reverse process of autoboxing.It converts the object of wrapper classes to the corresponding primitive data types.

Example:

Integer to int,Character to char etc.

6.What is oops concept ?

OOPs stands for object-oriented programming language.It provides the real-world entities such as Abstraction,Encapsulation,Polymorphism  and Inheritance.

7.What is the final keyword ?

Final is a key keyword which sets the restrictions on the variables,methods and classes.

Variables

When any variable makes  final ,its value cannot be changed.

Example

final String name=”Placement”;

Method

When any method makes final,it can not be overridden.

Example

final void message( )  ///can’t override this method

{
System.out.print(“Hello java”);

}

Class

When any class makes final,it can not be extended.

Example


final public class Test
{
}

public class Abc extends Test  ///can’t extend here
{
}

8.What is the difference between Abstract class and Interface ?

Interface

1.Java interfaces are implicitly abstract & do not contain implementation.

2.It contains only static and final variables

3.It contains default and static method also from java 8

4.It support multiple inheritance

5.”interface” keyword is used to declare interface

6.Using interface ,we can achieve 100% abstraction

Abstract class

1.Abstract class contains both abstract and non-abstract methods.

2.It contains final,non-final,static & non-static variables.

3.”Abstract” keyword is used to declare abstract method & define  abstract class

4.Using Abstraction,We  can achieve 0 to 100% abstraction.

5.It does not support multiple inheritance.

6.We extend the  class for the implementation of abstract methods.

9.Does java support multiple inheritance ?

Yes, java supports multiple inheritance indirectly means It supports with the help of interface,but we  can extend only one class in java. 

10.What do you mean by Method Overloading & Method Overriding ?

Method Overloading

When the class contains two or more methods with the same name but different arguments then this is known as method overloading.

Example:

Public class Test
{
in sum(int a,int b)
{
return a+b;
}

int sum(int a,int b,int c)
{
return a+b+c;
}
}

Method Overriding 

When a subclass or child class contains the same method with the same arguments of its parent class then this is known as Method Overriding.

Example:

public class ParentClass
{
void test( )
{
System.out.println(“This a overriding example”);
}
}

Public class ChildClass extends parentClass
{
public void test( )
{
System.out.println(“This is the base class example”);
}
public static void main(String[ ] args)
{
ChildClass obj=new ChildClass( );
obj.test( );
}
}

Output

This is the base class example 

11.Which class is the super class for all the classes ?

Object class is the super class for all the classes

12.What is the difference between Abstraction and Inheritance ?

Abstraction

Abstraction is the process of hiding the internal features and showing only functionality.

Example

When we send image or message via whats app then we don’t know that how it works internally

Inheritance

When a sub class /child class  acquires all the properties of the parent class then this process is known as Inheritance.

Example

In real life

Son/Daughter can use all  the things of  their parents such as money ,Land & house etc.

13.What do you mean by Singleton class in java ?

Singleton class provides the facility that only one object can be created & this object can be shareable to all the classes.

14.What do you mean by  String ?

String is an object that represents the sequence of characters.

Example

char [ ] arry={‘s’,’u’,’m’,’a’,’n’};

String s =new String(arry);

Output

suman

There are two ways in which we can create string in java

1.By string literal

2.By new keyword

1.By string literal

String s=”suman”;

2.By new keyword

String obj=new String(“suman”);

15.What is the difference between == & .equals( ) ?

== operator compares  the string  on the basis of the memory address(references)

Example:

String s=”suman”;

String obj=new String(“suman”);

String k=new String(“suman”);

String obj1=”kumar”;

if(s==obj)  output false

if(obj==k) output true

if(k==obj1) output false

.equals( ) operator compares the string on the basis of value.

Example:

if(s==obj) output true

if(obj==k) output true

if(s==obj1) output false

16.What is the difference between final ,finally and finalize ?

final

final is a keyword which is applied  restrictions on variables,methods and class

finally

Finally block is always executable whether exception occurs or not

finalize

Finaliza is used in garbage collections for the clean up processing

17.What do you mean by multi-threading & its life-cycle ?

When multiple threads are executed at same time then this process is known as multi-threading.

Example:

Playing games & Listening music at the same time on mobile phone or laptop etc.

Life-Cycle

New

Runnable

Running

Waiting

Terminated

18.What do you mean Exception handling & its type ?

Exception is an abnormal condition which disturb the normal flow of the program

There  are two types of exception

1.Checked Exception :Exception checked at the compile time.

Example

IOException,SQLException

2.Un-checked Exception:

Exception checked at the run-time.

Example

NullpointerException,ArithmeticException etc

19.What is the difference between throw and throws  ?

throw

Java throw keyword is used to throw an exception

It can be declared single exception

throws

Java throws keyword is used declare an exception

It can be declared the multiple exception

20.Why java is not a 100% object oriented programming language ?

Java is not a 100% object-oriented programming language because it supports primitive data types such as int ,char,float,double etc.

Best of Luck!!!!!

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *