Java Interview Questions

What is the difference between an interface and an abstract class?

An abstract class can have instance methods that implement a default behavior. An interface can only declare

constants and instance methods, but cannot implement default behavior. An class is abstract if it is defined with the keyword ‘abstract’ or if any

functions in the class are defined with the keyword ‘abstract’.

Describe synchronization with respect to multithreading.

With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared

resources. Without synchronization it is possible for one thread to modify a shared variable while another thread is in the process of using or updating

the same shared variable, which can lead to significant errors.

What are checked and unchecked exceptions?

A checked exception is some subclass of Exception (or Exception itself), excluding class RuntimeException and it’s

subclasses. Making an exception checked forces client programmers to deal with the possibility that the exception will be thrown. Unchecked

exceptions are RuntimeException and any of it’s subclasses. Class Error and it’s subclasses are also unchecked. With an unchecked exception

the compiler doesn’t force client programmers to catch or declare it in a ‘throws’ clause. Client programmers may not even know that the exception could

be thrown. Checked exception must be caught at compile time, runtime exceptions do not.

What is Overriding?

When a class defines a method using the same name, return type and arguments as a method in it’s superclass, the method in the

class overrides the method in the superclass. When the method is invoked for an object of the class it is the new definition of the method that is

called. Methods may be overridden to be more public, not more private.

What is the default value of an object reference declared as an instance variable?

The value will be null unless defined explicitly.

What are pass by reference and pass by value?

Pass by reference means the passing address itself rather than passing the value. Pass by value means passing a

copy of the value.

Are Objects in java passed by value or by reference?

Java only supports pass by value. With objects, the object reference itself is passed by value and so both the

original reference and parameter copy both refer to the same object.

What is serialization?

Serialization is the mechanism used to convert an object to a byte stream.

How do I serialize an object to a file?

The class whose instances are to be serialized shoule implement an interface Serializable. Then you pass the

instance to the Object OutputStream which is connected to a fileoutputstream. This will save the object to a file.

Which methods of Serializable interface should I implement?

The Serializable interface is an empty interface.

What are some common uses of serialization?

Objects must be serialized in order to be sent over a network or saved to disk.

What happens to object references during serialization?

The serialization mechanism generates an object graph for serialization. This is a recursive process – all

referenced objects are serialized along with the target object. Therefore all referenced objects should be serializable. If any o fthe

objects is not serializable then a NotSerializableException will be thrown.

What happens to the static fields of a class during serialization?Are these fields serialized as part of each serialized object?


Yes, the static fields do get serialized. If the static field is an object then it must have implemented the

Serializable interface. The static fields are serialized as part fo every object, but the commonness of the static fields across all of the instances is

maintained even after serialization.

What is the purpose of finalization?

The purpose of finalization is to give an unreachable object the opportunity to perform an cleanup processing before the object

is garbage collected.

What is the difference between static and non-static variables?

A static variable is associated with the class as a whole rather than with the specific instances of a class.

Non-static variables take on unique values with each object instance.

What does the keyword ‘final’ mean in front of a class, method and member variable?

Final classes cannot be extended. Final methods cannot be overridden. Final member variables cannot

be reassigned (assigned a new value).

What is the purpose of the keyword ‘transient’?

Transient member variables are not serialized with objects.

What can go wrong if you replace && w/& in the following code?

String a = null;

If (a != null && a.length() > 10) {….}