Intermediate Java Interview Questions

Here are the most commonly asked intermediate Java questions during job interviews.

Is super class instantiated when subclass is instantiated?

No, the object created is an instance of both the super class and subclass, but there’s no separate instance of the super class. In order to create a separate instance of the super class it must be instantiated directly.

The constructor of the super class is always called when the subclass is instantiated, although which constructor of the super c


What are exceptions? What is the difference between checked and unchecked exceptions?

Exceptions are errors in a program which disrupt the normal flow of code execution.

Checked exceptions are those which can be expected by the program due to various conditions, such as bad user input. These are handled using a try/catch block, and the flow of code execution is still in control of the programmer. If checked exceptions are not hand


Are two Strings containing identical text always equal in every way, every time?

No. If the Strings are instantiated using the String constructor, then they will have the same value but will reference distinct, and therefore different, objects.


So, writing S1 == S2 will be false when either S1, S2, or both are created using the String constructor, because they are pointing to different objects. Using `S1.equa


What happens when one thread modifies a HashMap while another thread is iterating the same map? What is the correct way to do this?

A HashMap is not a synchronized Map, meaning that it is not thread-safe. If a HashMap is modified by a thread while another thread is iterating it, a ConcurrentModificationException will be thrown.

There are two possible ways to do this correctly:


Explain the difference between Heap and Stack in Java.

The Heap Memory in Java is the space used by the JVM Runtime to allocate memory and store objects used throughout the program. Heap objects are shared across the entire program. The Heap has a special space called PermGen which is used for holding metadata about user classes and some other advanced functions.

The Stack Memory is used by the JVM fo


Is it recommended to use the final keyword in method parameters?

Yes, Java is a Pass-by-Value language, so all the variables are local to the current method. Changing the value of the method parameter will not result in any changes in the calling method. To avoid mistakes and make the code more readable, marking the method parameters final is the best approach. This also makes it mandatory for the developer to e


What is the significance of the Garbage Collector in Java? How does it work?

The Garbage Collector is an automatic memory management tool that reclaims memory by removing unused heap objects.


Explain the uses of this keyword.

this is a self-reference to the current instance of the class. It can be used to invoke the constructor, to reference a class-level variable or method, or to refer to or pass the object instance.

When this is used for calling the constructor within another constructor, it is called constructor chaining. It must be the first line of the constru


Can you create an object of a class within the same class? Can this lead to an error?

Yes, you can instantiate a class within itself. Example:

The line commented as Error line is an example of self-object instantiation leading to an error. In this case, the JVM will allocate stack space on each constructor call, and the stack will soon run out of space leading to a StackOverflowError, a common recursion problem.

Why won't the following code sample compile and how would you fix it?

This will raise a compilation error. It is incorrect to pass a List<String> to a method accepting List<Object>. A List<Object> can accept Strings, but t1 can only accept List<Object>.

Either ls should be changed to List<Object>, or lo should be made Generic: List<? extends Object> lo

What is super? Are the following uses of super correct?

The super keyword is used to reference the parent class and methods of the parent class. super can be used to invoke the constructor of the parent class as well. In this case, it must be the first line of code. If there is no default constructor in the base class, then it is mandatory to call one of the base class parameterized constructors.


Looking for developers?