Tricky Java Interview Questions Series: Release 4

Abhijit Jadhav
Javarevisited
Published in
3 min readJul 26, 2022

--

Another day another release of tricky java interview questions. You can go through Release 3 here. and the whole list here.

31. What is the best candidate for the HashMap Key? Are Strings a good candidate for HashMap Key?

Immutable objects are very good candidates for the HashMap Key, as they cannot be changed once created. As Strings are immutable in nature they are a very good candidate for the HashMap Key.

32. Can a variable be local and static at the same time?

No the variable cannot be local and static at the same time, doing so will give you a compilation error. Only final is permitted locally.

33. Is it possible to define a method in java class and provide its implementation in another programming language like C++?

Yes, you can do this by using the native method stack. In the case of native method stack development, we will define public static methods in our Java class without its implementation and then provide the implementation in another language like C++

34. Can we declare an abstract class final ?

No, we cannot declare the abstract class final. Logically the main aim of the abstract class is to provide the feasibility of having the abstract and non-abstract methods and if we declare the abstract class final then we will not be able to extend the abstract class and the whole scenario won't make any sense 😊.

// The below code will give compilaiton errors
public abstract final class Car{
}

35. Can we use a default constructor of a class even if an explicit constructor is defined?

If we have a parameterized constructor then we can not call the internal default constructor automatically, It would give a compilation error.

36. When a thread is executing synchronized methods then is it possible to execute other synchronized method simultanously by other thread?

No it is not possible to execute synchronized method by other threads when a thread is inside a synchronized method.

37. If two threads have same priority which thread will be executed first?

It will totally depends on thread scheduler to which thread to execute. The scheduler can do pick any thread from the pool and run it till it completes or it can give equal oppurtunity for all the threads by time slicing.

38. Can sleep() method causes another thread to sleep?

No sleep() method causes only the current thread to sleep not any other thread.

39. In context to thread, when will IllegalMonitorStateException will be thrown?

when wait(), notify(), notifyall() are called in non-synchronized context we will recieve a IllegalMonitorStateException.

40. Can we serialize static variables in java ?

We can’t serialize static variable in java. The reason being static variable are class that belong to a class not to object but serialization saves only object state not the class state.

We have Release 5 here 😊

--

--

Abhijit Jadhav
Javarevisited

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