Tricky Java Interview Questions Series: Release 3

Abhijit Jadhav
3 min readJul 21, 2022

We are back with another release of tricky interview questions for a quick interview revision and preparation. You can go through Release 2 here.

Photo by Van Tay Media on Unsplash

Hurray !!! in the further releases, we will also be covering answers to the questions.

Readers be like :

Credits: Unknown

21. Why String is immutable in nature?

The main reason for a String to be immutable is Security because strings are shared in different areas like flat file systems, networking, database connections, etc. Immutability allows the string to be safe and secure so that no one can change the references of the string once it gets created.

Another reason is to avoid changing the shared references from anywhere.

22. Can we use the assignment operator in the ‘if’ block.

Yes, variables can be assigned in the ‘if’ block but cannot be declared in the if block.

int i;
if((i = someMethod()) != 0) return true;

23. What is the difference between synchronized block and synchronized method?

There is no visible difference between both, both achieve the same objective. It's just what context they are used is making a difference. The synchronized keyword used at the method level makes the whole method thread-safe. Whereas synchronized block only makes the code written inside it thread-safe.

24. Can we have a static method and non-static method with the same name in single class?

No, we can not have a static method and non-static method with the same name in a single class, the compiler will give a compile time error. The below scenario is not allowed.

class Main{
public static void main(){
//some code
}
public void main(){
//some code
}
}

25. How can we make the main thread a Daemon thread?

Ideally, when we have to make thread daemon thread we use the setDaemon() method from the thread class but we cannot make the Main thread as the Daemon thread.

26. Can you use hashcode() default implementation in the Interface?

You cannot give your default implementation of the hashcode() in the interface for all the implementing classes to use.

We are not allowed to override java.lang.Object classes method as a default method in interface else will get a compile-time error.

27. How does the default method in the interfaces cope up with the diamond problem.

The diamond problem in java is when two interfaces contain the same default methods and implementing class gets confused about which method to call this is called a diamond problem in java. In such a situation, the code will not compile.

The solution to this is to override the ambiguous method and call Interface.super.methodName().

28. Are static methods in the interface available to the implementing classes by default?

Static methods are not available for the implementing classes by default. You can call these static methods using the interface name explicitly from the implementing class as implementing class won't have access to these methods directly.

29. Why does the PermGen replaced by MetaStore in Java Memory Management?

The issue with PerGen was it was very hard to predict how much size PermGen will need and MetaStore helps in improving the garbage collection performance.

30. Suppose if we have multiple classes in the same source file then what will happen during the compile time?

Suppose we have a .java file with class A, class B, and class C in the same while then during the compilation there will be a separate .class file created for each class even though they are in a single java file.

You can go through more just tricky interview questions in Release 4 here and don’t forget to follow me for more such questions.

--

--

Abhijit Jadhav

Full Stack Java Developer and AI Enthusiast loves to build scalable application with latest tech stack