Frequently asked Java interview questions – Part II

Frequently asked Java interview questions – Part II

In my previous post, I have discussed some of the frequently asked Java interview questions for an experience level of 2-5 years. In this post we will discuss some more questions and answers.

1. Difference between ArrayList and HashSet?

This question may look simple but is one of the important java interview questions. The way you answer is really important.

Implements the List interfaceImplements the Set interface
ArrayList is an ordered collection, which means it stores and displays elements in the order they were addedHashSet is an unordered collection, it doesn’t guarantee any order.

Note: LinkedHashSet maintains the elements in insertion order.

Allows duplicate elementsDoesn’t allow duplicate elements
Can contain any number of null valuesAt most can contain one null value.
ListIterator can be used to traverse ArrayList in both the directions(forward and backward)There is no iterator to traverse a HashSet in both directions
ArrayList can be accessed by index using the method get (index).HashSet doesn’t provide get() method
ArrayList can be converted to an array by calling the toArray() methodHashSet doesn’t provide method to convert to an array

2. What is a JVM? What is the difference between JRE and JVM?

A Java virtual machine (JVM) is a virtual machine that can execute Java byte code. It is the code execution component of the Java platform. Oracle’s Java execution environment is termed the Java Runtime Environment, or JRE. Programs intended to run on a JVM must be compiled into Java byte code, a standardized portable binary format which typically comes in the form of .class files (Java class files). Source code is compiled to Java byte code, which is verified, interpreted or JIT-compiled for the native architecture. The Java APIs and JVM together make up the Java Runtime Environment (JRE).

java interview questions

As it name specifies, it is a virtual computer placed on top of your hardware and OS. Whenever a java application is called, a JVM instance is created. The thread from OS creates the JVM instance.

Inside the JVM, the Class loader subsystem loads your compiled java byte code to the memory. The JVM creates the instance of your class object. The object instance is stored in the heap. The non-demon thread executes your methods inside your object instance and finally the non-demon thread is destroyed and the JVM instance is also ended.

3. What is String constant pool? Will the string constant pool be present when the JVM is just started?

Refer my previous post Strings in Java to understand about strings and string constant pool.

4. What is the difference between throw and throws keyword?

This is one of the most important java interview questions. Let see the difference.

Sometimes we can create exception object and we can handover that exception object to the JVM explicitly by throw keyword. The main purpose of throw keyword is to handover our created exception object explicitly to the JVM.The main purpose of throws keyword is to delegate the responsibilities of exception handling to the caller (method). It can be used to propagate checked exceptions.
Throw is used within the method body to invoke an exceptionThrows is used in the method signature. When a method declares throws clause, the caller should handle this exception otherwise it will show a compile time error.
You cannot throw multiple exceptions with throw keyword.You can declare multiple exceptions in the throws clause e.g. public void method () throws SQLException, IOException.
throw is followed by an instance of Exceptionthrows is followed by an exception class name

5. What is LinkedList in Java? What is the difference between ArrayList and LinkedList?

Resizable-array implementation of the List interface.Doubly-linked list implementation of the List and Deque interfaces
Frequent insertion/removal of elements can be slower based on the index. This is because the elements have to be shifted in the array.Frequent insertion/removal of elements is faster. This is because removal only requires change in the pointer location in the two neighbor nodes (elements) of the node which is going to be removed.
Search operation is faster in ArrayList because it maintains index based system for its elements.Search operation is slower because it requires traversing through all the elements for searching an element.
Memory consumption is comparatively lowMemory consumption is high because of the additional pointers required for each element to maintain the structure.
Supports only List operationsSupports both list and queue operations like poll(), peek() etc.

Java doc – LinkedList, ArrayList.

6. Can we override static method?

Static methods cannot be overridden in the exact sense of the word, but they can hide parent static methods.

In practice it means that the compiler will decide which method to execute at compile time, and not in runtime, as it does with overridden instance methods. That’s what we mean when we say a static method does not have run-time polymorphism. Instance methods can only participate in method overriding.

7. What is a Singleton class? How to create a Singleton class in Java?

A singleton class is a class that can be instantiated once, and only once. This means that there can be only one object of the singleton class at any point of time. This is a fairly unique property, but useful in a wide range of object designs. Creating an implementation of the singleton pattern is fairly straightforward – simply block off access to all constructors, provide a static method for getting an instance of the singleton, and prevent cloning. The class, itself doesn’t allow to create second object is called singleton class.

The Singleton Design Pattern is a Creational type of design pattern which assures to have only a single instance of a class.



8. Can static variables be serialized?

Static variables are not serialized.

serialVersionUID is a special static variable used by the serialization and deserialization process, to verify that a local class is compatible with the class used to serialize an object. It’s not just a static variable as others, which are definitely not serialized.

When an object of a class is serialized, the class name and serial version UID are written to the stream of bytes. When it’s deserialized, the JVM checks if the serial version UID read from the stream of bytes is the same as the one of the local class. If they’re not, it doesn’t even try to deserialize the object, because it knows the classes are incompatible.

9. What do you mean by cloning? How to make an object cloneable? Explain deep and shallow copy.

Cloning is nothing but making a copy of a object. In order for the object to be cloneable, the class should implement the marker interface Cloneable. Read my previous post to understand about shall copy and Deep copy.

10. What does transient means or what is a transient variable?

transient is a Java keyword which marks a member variable not to be serialized when it is persisted to streams of bytes. When an object is transferred through the network, the object needs to be ‘serialized’. Serialization converts the object state to serial bytes. Those bytes are sent over the network and the object is recreated from those bytes. Member variables marked by the java transient keyword are not transferred; they are lost intentionally.

You should serialize variables that you wish to be able to restore that you would otherwise be incapable of restoring programmatically. Similarly, you should ensure that you’re not saving data unnecessarily; if a variable can be easily derived based on processing of other serializable data, you can mark it as transient.

11. Explain Autoboxing and Unboxing.

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. The reverse process is called unboxing (Integer to int etc).

Here is the simplest example of autoboxing and unboxing:

What happens behind the scenes?
In the first statement what compiler does is,

and in the second statement,


Hope you find these questions useful. We will discuss more questions in upcoming posts. If you have any doubt post it in the comments section.

The following two tabs change content below.
Working as a Java developer since 2010. Passionate about programming in Java. I am a part time blogger.

Add Comment

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