INTERVIEW QUESTIONS » IMPORTANT Java interview questions and answers for freshers

IMPORTANT Java interview questions and answers for freshers

Java’s First Name OAK.

James Gasling is discovered java in 1991.

JVM is an acronym for Java Virtual Machine, it is an abstract machine which provides the runtime environment in which java byte code can be executed.

JRE stands for Java Runtime Environment.
It is environment where JVM executes the byte code.
It is physically exists.

JVM stands for Java Virtual Machine.
It is an abstract machine where java byte code can be executed.
It is available for many software and hardware platforms.
JVM is platform dependent


IDE stands for Integrated Development Environment:
Use editor software like:
Notepad ++
Text pad
Edit plus
Use Integrated Development Environment Tools like:
J Developer
Net Beans
IntelliJ IDEA

Java is high level language and object oriented Programming Language.

Java is the both compiled and interpreter language.

Compiler is a program that transforms source code written in a programming language into binary format (or)executable program.

Interpreter executes the source code directly (or) translates the code one line at a time. It generates the binary code, but the code is never compiled instead interpreter each and every time the program executes.

We can compile into class fileforany machine and run it on the any machine has JVM.

Java is a platform independent language.

Platform independence means that we can write and compile the java code in one platform (eg Windows) and can execute the class in any other supported platform eg (Linux,Solaris,etc).

JDK is Java Development Kit which is for development purpose and it includes execution environment also. But JVM is purely a run time environment and hence you will not be able to compile your source files using a JVM.

Pointer is a reference handle to a memory location. Improper handling of pointers leads to memory leaks and reliability issues hence Java doesn’t support the usage of pointers.

Java doesn’t support multiple inheritance.

Java uses primitive data types and hence is not a pure object oriented language.

In Java, Arrays are objects.

Array can hold multiple values of same datatypes. It is an objects in java.

Path and Classpath are operating system level environment variales. Path is used define where the system can find the executables(.exe) files and classpath is used to specify the location .class files.

Local varaiables are those which are declared within a block of code like methods. Local variables should be initialised before accessing them.

Main() method doesn’t return anything hence declared void.

main() method is called by the JVM even before the instantiation of the class hence it is declared as static.

A protected method can be accessed by the classes within the same package or by the subclasses of the class in any package.

A final variable’s value can’t be changed. final variables should be initialized before using them.

A method declared as final can’t be overridden. A sub-class can’t have the same method signature with a different implementation.

When a method needs to be accessed even before the creation of the object of the class then we should declare the method as static.

A Class which doesn’t provide complete implementation is defined as an abstract class. Abstract classes enforce abstraction.

Not possible. An abstract class without being inherited is of no use and hence will result in compile time error.

Variables can’t be declared as abstract. only classes and methods can be declared as abstract.

Constructor is just like a method that is used to initialize the state of an object. It is invoked at the time of object creation.

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.

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

A static method belongs to the class rather than object of a class.
A static method can be invoked without the need for creating an instance of a class.
static method can access static data member and can change the value of it.

No, you can’t override the static method because they are the part of class not object.

It is because the static method is the part of class and it is bound with class whereas instance method is bound with object and static gets memory in class area and instance gets memory in heap.

No, because methods of an interface is abstract by default, and static and abstract keywords can’t be used together.

String is an immutable object. StringBuffer is a mutable object.

StringBuffer is synchronized whereas StringBuilder is not synchronized.

We can create immutable class as the String class by defining final class

Garbage collection is a process of reclaiming the runtime unused objects.It is performed for memory management.

gc() is a daemon thread.gc() method is defined in System class that is used to send request to JVM to perform garbage collection.

finalize() method is invoked just before the object is garbage collected.It is used to perform cleanup processing.

The purpose of the Runtime class is to provide access to the Java runtime system.

– Static variables are the variables which has exactly one copy per class. They belong to Downcasting is the casting from a general to a more specific type, i.e. casting down the hierarchy

Variable is named memory location which can holds the value and the value can be changed any number of times during execution and it is also identifier in java.

Basically string is a class and its variable can hold data of type string. Since the variable is derived from the class and can holds by data. This is called derived data type.

Object is an instance of a class. We can create an object by using new keyword.

Method is a set of code which can be called at any point in a program simply by utilizing the method’s name. Method can have return a value. It can represents behaviour of the object or class.

When we have more than one constructor for a class, we can call it as the constructor overloading.
When we overload the constructors, the number of arguments should be different (or) types of arguments should be different (or) sequence of arguments should be different.

Methods are blocks used for code re usability.
Constructors are blocks used to build the object of the class.
Method name are identifiers which represents the behaviour of the class.
Constructor name should be same with as class name.
Methods have return types.
Constructors does not have return types.
In method return statement are used.
In constructor return statement are not used.

If we need to create an object of a class based on the specific criteria

When some of the functionalities are common among the implementing classes and some are class specific then we can go with abstract class.

When all the functionalities are common to the implementing classes and implementation are class specific then we can go with interface

Converting lower data type to the any of the higher data type is called as widening, since widening is done automatically by compiler it is also called the auto widening.

Converting higher data type to lower data type is called as explicit narrowing. Compiler does not know to do narrowing. It should be explicitly specified in the program

Synchronized block is used to lock an object for any shared resource.
Scope of synchronized block is smaller than the method.

The notify() is used to unblock one waiting thread whereas notifyAll() method is used to unblock all the threads in waiting state.

Deadlock is a situation when two threads are waiting on each other to release a resource. Each thread waiting for a resource which is held by the other waiting thread.

A thread is a lightweight subprocess.It is a separate path of execution.It is called separate path of execution because each thread runs in a separate stack frame.

Multithreading is a process of executing multiple threads simultaneously. Its main advantage is:
• Threads share the same address space.
• Thread is lightweight.
• Cost of communication between process is low.

A while statement checks at the beginning of a loop to see whether the next loop iteration should occur. A do while statement checks at the end of a loop to see whether the next iteration of a loop should occur. The do whilestatement will always execute the body of a loop at least once.

A public class may be accessed outside of its package. A non-public class may not be accessed outside of its package.

The if statement is used to select among two alternatives. It uses a boolean expression to decide which alternative should be executed. The switch statement is used to select among multiple alternatives. It uses an int expression to determine which alternative should be executed.

Overriding is a method with the same name and arguments as in a parent, whereas overloading is the same method name but different arguments

A break statement results in the termination of the statement to which it applies (switch, for, do, or while). Acontinue statement is used to end the current loop iteration and return control to the loop statement.

Wrapper classes are classes that allow primitive types to be accessed as objects.

If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding. It is used for runtime polymorphism and to provide the specific implementation of the method.

Interface is a blueprint of a class that have static constants and abstract methods.It can be used to achieve fully abstraction and multiple inheritance

Exception Handling is a mechanism to handle runtime errors.It is mainly used to handle checked exceptions.

1)Checked Exception
The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.IOException,SQLException etc. Checked exceptions are checked at compile-time.
2)Unchecked Exception
The classes that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException,NullPointerException etc. Unchecked exceptions are not checked at compile-time.

The toString() method returns the string representation of any object. If you print any object, java compiler internally invokes the toString() method on the object. So overriding the toString() method, returns the desired output, it can be the state of an object etc. depends on your implementation.

Singleton class means that any given time only one instance of the class is present, in one JVM.

An applet is a small java program that runs inside the browser and generates dynamic contents.

JDBC is a Java API that is used to connect and execute query to the database. JDBC API uses jdbc drivers to connects to the database.

JDBC Driver is a software component that enables java application to interact with the database.

There are 4 types of JDBC drivers:
1. JDBC-ODBC bridge driver
2. Native-API driver (partially java driver)
3. Network Protocol driver (fully java driver)
4. Thin driver (fully java driver)

• Registering the driver class
• Creating connection
• Creating statement
• Executing queries
• Closing connection

There are 3 JDBC statements.
1. Statement
2. PreparedStatement
3. CallableStatement

By using PreparedStatement interface, we can store and retrieve images.

If statements are used to evaluate Boolean expression.
If we are using only values (list of values or 1-1 mapping) we can go for the switch case.

These methods can be used as a hint to the JVM, in order to start a garbage collection. However, this it is up to the Java Virtual Machine (JVM) to start the garbage collection immediately or later in time.

final is a modifier which can be applied to a class or a method or a variable. final class can’t be inherited, finalmethod can’t be overridden and final variable can’t be changed.

finally is an exception handling code section which gets executed whether an exception is raised or not by the try block code segment.

finalize() is a method of Object class which will be executed by the JVM just before garbage collecting object to give a final chance for resource releasing activity.


Class cannot be declared as private.
Constructor can be declared as private.

Class can be declared as final.
Constructor cannot be declared as final.

During its execution, a thread can reside in one of the following states: NEW: The thread becomes ready to run, but does not necessarily start running immediately. RUNNABLE: The Java Virtual Machine (JVM) is actively executing the thread’s code. BLOCKED: The thread is in a blocked state while waiting for a monitor lock. WAITING: The thread waits for another thread to perform a particular action. TIMED_WAITING: The thread waits for another thread to perform a particular action up to a specified waiting time. TERMINATED: The thread has finished its execution. 13. What is the difference between a synchronized method and a synchronized block? In Java programming, each object has a lock. A thread can acquire the lock for an object by using the synchronized keyword. The synchronized keyword can be applied in a method level (coarse grained lock) or block level of code (fine grained lock).

C++ :
C++ is basically C with Object-oriented extension
C++ supports operator overloading
C++ supports multiple inheritance of classes

C++ supports structure, pointers and unions
C++ supports method overloading
C++ does not support the constructor overloading
C++ has template classes


Java is true Object-oriented language
Java does not support operator overloading
Java does not support multiple inheritance of classes but it supports interface
Java does not supports structure, pointers and unions
Java supports method overloading as well as method overriding
Java supports the constructor overloading
Java does not have template classes

throw keyword:

throw is used to explicitly throw an exception.
checked exceptions can not be propagated with throw only.

throw is followed by an instance.
throw is used within the method.

throws keyword:
throws is used to declare an exception
checked exception can be propagated with throws.
throws is followed by class.
throws is used with the method signature

Abstract class:

An abstract class can have method body (non-abstract methods).

An abstract class can have instance variables.
An abstract class can have constructor.
An abstract class can have static methods.
You can extends one abstract class.

Interface have only abstract methods
An interface cannot have instance variables.
Interface cannot have constructor.
Interface cannot have static methods.
You can implement multiple interfaces


The wait() method is defined in Object class.
wait() method releases the lock.

The sleep() method is defined in Thread class
The sleep() method is defined in Thread class


ArrayList uses a dynamic array.
ArrayList is not efficient for manipulation because a lot of shifting is required.
ArrayList is better to store and fetch data.

LinkedList uses doubly linked
LinkedList is efficient for manipulation.
LinkedList is better to manipulate data.


Iterator traverses the elements in forward direction only.
Iterator can be used in List, Set and Queue.

ListIterator traverses the elements in backward and forward directions both.
ListIterator can be used in List only.


HashMap is not synchronized.

HashMap can contain one null key and multiple null values.

Hashtable is synchronized.
Hashtable cannot contain any null key or null value

Comparable :

Comparable provides only one sort of sequence.
It provides one method named compareTo().

It is found in java.lang package.
If we implement Comparable interface, actual class is modified.

Comparator provides multiple sort of sequences.
It provides one method named compare
It is found in java.util package.
Actual class is not modified

Abstract class :

Abstract class should be declared with keyword abstract.

Abstract class contains both concrete and abstract methods.

Abstract class we can use all the access specifier.

Interface :
Interface starts with keyword interface and by default it is abstract.
Interface contains only the non-static abstract methods.
Interface we can use only the public access specifier.