Java Interview Questions


Prev Tutorial Next Tutorial

Interview Questions

Constructor

Why use constructor ?

The main purpose of create a constructor is, for placing user defined values in place of default values.

Why constructor not return any value ?

Constructor will never return any value even void, because the basic aim constructor is to place value in the object

Why constructor definition should not be static ?

Constructor definition should not be static because constructor will be called each and every time when object is created. If you made constructor is static then the constructor before object creation same like main method.

Why constructor is not inherited ?

Constructor will not be inherited from one class to another class because every class constructor is created for initialize its own data members.

What is purpose of default constructor ?

The purpose of default constructor is to create multiple object with respect to same class for placing same value.

What is purpose of parameterized constructor ?

The purpose of parametrized constructor is to create multiple object with respect to same class for placing different value of same type or different type or both.

Is constructor inherited?

No, constructor is not inherited.

Can you make a constructor final?

No, constructor can't be final.

What is the purpose of default constructor?

The default constructor provides the default values to the objects. The java compiler creates a default constructor only if there is no constructor in the class.

Does constructor return any value?

yes, that is current instance (You cannot use return type yet it returns a value).

What is flow of constructor in Java ?

Constructor are calling from bottom to top and executing from top to bottom.

Why overriding is not possible at constructor level. ?

The scope of constructor is within the class so that it is not possible to achieved overriding at constructor level.

Difference between Method and Constructor

MethodConstructor
1Method can be any user defined nameConstructor must be class name
2Method should have return typeIt should not have any return type (even void)
3Method should be called explicitly either with object reference or class referenceIt will be called automatically whenever object is created
1Method is not provided by compiler in any case.The java compiler provides a default constructor if we do not have any constructor.

String Handling

Why use string handling in Java

The basic aim of String Handling concept is storing the string data in the main memory (RAM), manipulating the data of the String, retrieving the part of the String etc. String Handling provides a lot of concepts that can be performed on a string such as concatenation of string, comparison of string, find sub string etc.

Difference between String and StringBuffer

What is difference between equal() and == ?

equals() method always used to comparing contents of both source and destination String. It return true if both string are same in meaning and case otherwise it returns false.

== Operator is always used for comparing references of both source and destination objects but not their contents.

StringStringBuffer
1The data which enclosed within double quote (" ") is by default treated as String class.The data which enclosed within double quote (" ") is not by default treated as StringBuffer class
2String class object is immutableStringBuffer class object is mutable
3When we create an object of String class by default no additional character memory space is created.When we create an object of StringBuffer class by default we get 16 additional character memory space.

When we use String, StringBuffer and StringBuilder

  • If the content is fixed and would not change frequently then we use String.
  • If content is not fixed and keep on changing but thread safety is required then we use StringBuffer
  • If content is not fixed and keep on changing and thread safety is not required then we use StringBuilder

What is Similarities between String and StringBuffer

  • Both of them are belongs to public final. so that they never participates in inheritance that is is-A relationship is not possible but they can always participates in As-A and Uses-A relationship.
  • We can not override the method of String and StringBuffer.

Difference between StringBuffer and StringBuilder

All the things between StringBuffer and StringBuilder are same only difference is StringBuffer is synchronized and StringBuilder is not synchronized. synchronized means one thread is allow at a time so it thread safe. Not synchronized means multiple threads are allow at a time so it not thread safe.

StringBufferStringBuilder
1It is thread safe.It is not thread safe.
2Its methods are synchronized and provide thread safety.Its methods are not synchronized and unable to provide thread safety.
3Relatively performance is low because thread need to wait until previous process is complete.Relatively performance is high because no need to wait any thread it allows multiple thread at a time.
1Introduced in 1.0 version.Introduced in 1.5 version.

What is StringTokenizer ?

It is a pre defined class in java.util package can be used to split the given string into tokens (parts) based on delimiters (any special symbols or spaces). Read more......

Exception Handling

What is Exception Handling ?

The process of converting system error messages into user friendly error message is known as Exception handling.

What is Exception ?

An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's Instructions.

Which is super class for any Exception class ?

Object class is super class for any Exception class.

Can any statement is possible between try and catch block ?

Each and every try block must be immediately followed by catch block that is no intermediate statements are allowed between try and catch block.

Can any try block contain another try block ?

Yes, One try block can contains another try block that is nested or inner try block can be possible.

When IOException is thrown ?

IOException is thrown in following conditions which is given below;

  • Try to transfer more data but less data are present.
  • Try to read data which is corrupted
  • Try to write but file is read only.

When ArithmeticException is thrown ?

The ArithmeticException is thrown when integer is divided by zero or taking the remainder of a number by zero. It is never thrown in floating-point operations.

Difference between throw and throws ?

throwthrows
1throw is a keyword used for hitting and generating the exception which are occurring as a part of method bodythrows is a keyword which gives an indication to the specific method to place the common exception methods as a part of try and catch block for generating user friendly error messages
2The place of using throw keyword is always as a part of method body.The place of using throws is a keyword is always as a part of method heading
3When we use throw keyword as a part of method body, it is mandatory to the java programmer to write throws keyword as a part of method headingWhen we write throws keyword as a part of method heading, it is optional to the java programmer to write throw keyword as a part of method body.

Difference between checked Exception and un-checked Exception ?

Checked ExceptionUn-Checked Exception
1checked Exception are checked at compile timeun-checked Exception are checked at run time
2for checked Exception Extend Throwable class except RuntimeException.for un-checked Exception extend RuntimeException.
3e.g.
IOException, SQLException, FileNotFoundException etc.
e.g.
ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException, NumberNotFoundException etc.

Difference between Error and Exception

ErrorException
1Can be handle.Can not be handle.
2Example:
NoSuchMethodError
OutOfMemoryError
Example:
ClassNotFoundException
NumberFormateException

Multithreading

What is thread ?

Thread is a lightweight components and it is a flow of control. In other words a flow of control is known as thread.

What is multithreading ?

Multithreading in java is a process of executing multiple threads simultaneously.

Explaing State or Life cycle of thread.

State of a thread are classified into five types they are

  • New State
  • Ready State
  • Running State
  • Waiting State
  • Halted or dead State

Read more.........

How to achieve multithreading in java ?

In java language multithreading can be achieve in two different ways.

  • Using thread class
  • Using Runnable interface

In which state no memory is available for thread ?

If the thread is in new or dead state no memory is available but sufficient memory is available if that is in ready or running or waiting state.

Difference between sleep() and suspend() ?

Sleep() can be used to convert running state to waiting state and automatically thread convert from waiting state to running state once the given time period is completed. Where as suspend() can be used to convert running state thread to waiting state but it will never return back to running state automatically.

What is Thread Synchronization ?

Allowing only one thread at a time to utilized the same resource out of multiple threads is known as thread synchronization or thread safe.

Why use Thread Synchronization ?

Whenever multiple threads are trying to use same resource than they may be chance to of getting wrong output, to overcome this problem thread synchronization can be used.

How to achieve Thread Synchronization in java ?

In java language thread synchronization can be achieve in two different ways.

  • Synchronized block
  • Synchronized method

How to achieve multiple inheritance in java ?

Using Interface concept you can achieve multiple inheritance in java.


Prev Tutorial Next Tutorial

Advertisements
Download Projects