Tricky Java Interview Questions Series: Release 5

Tricky Java Interview Questions Series: Release 5

41. How can we make sure that thread t1,t2, and t3 will execute in the same sequence in java?

We can achieve this by using the join() method of the there. We need to make sure that t3 calls t2.join() and t2 will call t1.join(). Then t1 will get a chance to execute first and t3 will execute at the end.

42. Write a stream API to count the number of occurrences in the String.

String string = "java springboot springcode spring java";List<String> list = Arrays.asList(string.split(" "));Map<String, Long> collect =,Collectors.counting()));System.out.println(collect.toString());
{spring=1, java=2, springcode=1, springboot=1}

43. Between Thread and Runnable which is a better approach to create a thread and Why?

When we extend the Thread class then each of our Thread creates a unique object associated with it and When we implement Runnable it shares the same object with multiple threads.

44. What do you mean when you say Java is a statically-typed language?

It means that all the variables must be declared before we can assign value to it.

45. What is the problem with the equals() method in inheritance that violates the symmetric behavior in Java and how to resolve it?

The answer is given in this post

46. Can we use stream API directly on the array instead of Collections?

Yes, we can apply stream API on array also. We can use Stream.of() method which will convert them to stream so that multiple stream operations can be applied.

Stream.of(1,11,111,1111).filter(i -> i > 11).forEach(System.out::println);

47. What is an atomic operation in java ?

An atomic action cannot be suspended in the middle of being executed. It either completes or it doesn’t happen at all. Once a thread starts to run an atomic action, we can be confident that it won't be suspended until it has completed the action.

  • Reading and writing reference variable: For example, MyObj1 = MyObj2 would be atomic. A thread can’t be suspended in the middle of executing this statement.
  • Reading and writing primitive variables except those of type long and double.
myInt = 10 // thread cannot be suspended
myDouble = 1.234 // thread can be suspended
  • Reading and writing all variables declared volatile.

48. How can we test a method that returns a void?

We can use verify() method which lets us verify whether the mock void method is being called or not.

49. What annotation is used to specify the composite primary key?

@EmbeddedId is used to specify the composite primary key

50. How can we access the environment variable in the java code.

We can use System.getenv() to access the environment variable.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhijit Jadhav

Abhijit Jadhav


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