Advanced Java Interview Questions

These advanced Java interview questions are often given in practicals.

Should you use external libraries in your code? What are the benefits and drawbacks? What precautions can be taken?


External libraries are extremely helpful when writing large and complex programs - they save the effort of having to code something which has already been implemented by someone else.

...

What are Immutable Classes in Java? What are Immutable Classes useful for?


Immutable classes are those which cannot be changed once instantiated. The value of the object of an immutable class will always remain the same. Either the value cannot be changed or any change to the vale will result in a new immutable instance. String, Boolean and other primitive wrapper classes are all Immutable. All the variables and the c

...

What parts of each line of the following code will be stored in the Heap and what parts will be stored in the Stack?


Before the main method is called, the program allocates Heap space and puts all the runtime classes and class-level objects into the Heap.

Line 1 - Creates stack space for the main method. The String arguments are in the Heap in the PermGen area, while references to the strings are stored in the Stack.

Line 2 - Puts the primitive x on the Sta

...

Explain why Java is Pass-by-Value and not Pass-by-Reference. Give an example.


Pass-by-value is when the reference to an object is copied to a new variable.

Pass-by-reference is when a reference to an object is passed directly to the other method.

When a method is called in Java, new Stack space is allocated for the method and all variables in the arguments are copied to the new Stack block.

Example:

In the first print

...

Does a HashMap Key object have to be Immutable?


HashMap calculates the location of an object based on the value of the hashCode function. If the values used to calculate the hashCode of an object change after it is inserted in the HashMap, this will result in the Key returning a different address than the one originally used. In this case, the HashMap cannot locate the original Key and the objec

...

What are the automated memory management features of Java?


In comparison to other languages, the Java Runtime has several automated memory management features to improve the safety, reliability, efficiency, and ease of programming.

...

Describe a potential way of creating a memory leak in Java. Give an example.


Because Java has automated memory management, it precludes code errors relating to allocation of memory for objects and reclaiming memory from objects. This prevents user errors arising due to incorrect code for memory management, but it does not avoid design flaws that can result in memory leaks.

Example of Design flaw:

We are required to implem

...

If a super class method is throwing RuntimeException, can it be overriden in a subclass method throwing NullPointerException?


In Java, it is possible to override a method with a different throws signature if the base class method only throws Runtime Exception (unchecked exceptions). If the base class method throws checked exceptions, then the subclass method has to have one of the following:

What is type erasure?


The Type information used for Generic objects is only available at compile time. The compiler removes all the type information when converting to byte-code. This makes the type information unavailable at Runtime and the Generic type is converted to Raw type. The compiler provides checks at compile time to prevent runtime errors for incorrect type i

...

Is the following code compile-time and run-time safe?


Yes, this code is compile-time and run-time safe.

I and K are Generic type parameters used to ensure type safety.

The extends keyword in the first line defines the scope of the I parameter. The I parameter can only be a sub-type of List of type K. K by default extends Object.

The second extends keyword is used for inheriting c

...

What is the use of the volatile keyword in Java? What is the happens-before guarantee?


The volatile keyword is used in multi-threaded programs. It indicates that a variable can be updated by multiple threads. This means that the value of the variable cannot be cached locally and must be directly accessed from main memory every time. volatile variables are not serializable.

The happens-before guarantee for volatile variables ca

...

Explain instruction reordering. In what context can instruction reordering create problems? How can it be fixed?


Instruction reordering is when the execution order of the code is changed by the JVM at compile time or run time. This is done to optimize the code. Instruction reordering guarantees program order execution. This means that two statements which are related will have a happens-before relation, but two unrelated statements can be executed out-of-orde

...

Looking for developers?

Want to learn more about Gigster? Enter your email and we'll be in touch.