Java Interview Questions

Java Interview Questions and Answers

Below is a good collection of Java Interview Questions and Answers which will help you to prepare for any interview related to Java technology.

What is the name of the class that is super class for every class?


Is it possible to extend the java.lang.String class?

No. java.lang.String is declared as final. Class declared as final cannot be used as a parent class in inheritance.

What is the difference between a static and non-static method?

A static method is associated with the class as a whole rather than a specific instance of a class. Whereas a non – static method can be called only through an object


What is the purpose of garbage collection in java?

The purpose of garbage collection is to identify and discard objects that are no longer referenced by a program so that they can be reclaimed and reused.

How many public classes are permitted within a single java class file?

We can define only one public class within a single java class file.

What is the command line utility used to compile java source code into bytecode?

‘javac’ is the command line utility to compile java source code.

Why is the main() method always declared as a static method?

The main() method is declared as a static method so that it can be invoked without having to create an instance of the corresponding class.

How are constants declared in java?

Declaring a variable as ‘final’ makes the variable constant.

What is an object wrapper?

An object wrapper is a set of java classes that are used to change basic data type such as int or float into Objects.

Eg. java.lang.Integer, java.lang.Float etc.

How are command line arguments passed in java?

The command line arguments are passed as an array of Strings. If the application requires some input in any particular data type format then the string passed should

be type casted to the required data type.

What is static initializer code ?

A class can have a block of initializer code that is simply surrounded by curly braces and labeled as static e.g.
public class Demo{
static int =10;
System.out.println(“Hello world”);
And this code is executed exactly once at the time of class load

Where is native modifier used?

It can refer only to methods and it indicates that the body of the method is to be found else where and it is usually written in non java


What are transient variables?

A transient variable is not stored as part of objects persistent state and they cannot be final or static

What is synchronized modifier used for ?

It is used to control access of critical code in multithreaded programs

What are volatile variables?

It indicates that these variables can be modified asynchronously

What are the rules for Object reference casting ?

Casting from Old types to Newtypes

Compile time rules

When both Oldtypes and Newtypes are classes, one should be subclass of the other

When both Oldtype ad Newtype are arrays, both arrays must contain reference types (not primitive), and it must be legal to cast an element of Oldtype to an element

of Newtype

You can always cast between an interface and a non-final object

Runtime rules

If Newtype is a class. The class of the expression being converted must be Newtype or must inherit from Newtype

If NewType is an interface, the class of the expression being converted must implement Newtype

When do you use continue and when do you use break statements ?

When continue statement is applied it prematurely completes the iteration of a loop. When break statement is applied it causes the entire loop to be abandoned.

What is the base class from which all exceptions are subclasses?

All exceptions are subclasses of a class called java.lang.Throwable

How do you intercept and thereby control exceptions ?

We can do this by using try/catch/finally blocks

You place the normal processing code in try block.

You put the code to deal with exceptions that might arise in try block in catch block

Code that must be executed no matter what happens must be place in finally block

What are the rules for primitive arithmetic promotion conversion ?

For Unary operators :

If operant is byte, short or a char it is converted to an int
If it is any other type it is not converted

For binary operands :

If one of the operands is double, the other operand is converted to double

Else If one of the operands is float, the other operand is converted to float

Else If one of the operands is long, the other operand is converted to long

Else both the operands are converted to int