Tricky Java Interview Questions Series: Release 6

Abhijit Jadhav
4 min readAug 6, 2022

Read Release 5 first or you can get the whole list here.

Tricky Java Interview Questions Series: Release 6

51. What is “out” in “System.out.println()”?

The System is the final class that comes from java.lang package. out is an instance variable of PrintStream type, which is a public static member of the System class. println() is a method available in the System class. This method prints any argument passed to it and adds a new line to the output.

52. What do you understand by the payload in the request?

The payload in the HTTP request or response consists of HTTP protocol information such as headers, a URL, body content, and version and status information. In simple words, It contains the data that you send to the server when you make an API request.

53. What are the major important files used in configuring JWT-based Spring Security?

The four main important files for configuring JWT-based Spring Security are

  1. AuthenticationException.java
  2. JWTAuthenticationRestController.java
  3. JWTTokenRequest.java
  4. JWTTokenResponse.java

54. Suppose we have 100 microservices that are communicating with each other and due to some issue in one of the microservices the application stopped working. In this scenario which Spring cloud utility will you use the trace the request and get the logs?

In this scenario will take the help of some Distributed Tracing server like Zipkin. Zipkin will help us to trace the request coming in and out, from or to the microservice by assigning them a unique identifier with the help of sleuth dependency. Zipkin also helps us with a UI to do the analysis of the request.

55. Can we test the private and static methods using Mockito?

There is no direct support in Mockito to rest the private and static methods. We can use an extension of the Mockito that is PowerMock to test the private and static methods in java.

56. What is the use of @Transactional in Spring?

@Transactional makes sure that the data is persisted properly in the database and database is consistent after the transaction has been executed.

Let's understand this with an example.

Suppose you are working on an amazon website and it has around 1000 microservices running and you have payment microservice and placeOrder microservice. Now you are in a scenario where we are not using @Transactional. Now what happens if the payment microservice is successfully able to do payment and data is persisted in the payment database. But the placeOrder microservice fails due to some exception in it and now user is has done the payment but his order is not placed.

So @Transactional solves this issue it makes sure that either both the transaction will happen or none of them will happen. In the above scenario the @Transactional will roll back the payment microservcie transaction and maintain the data consistency in the database.

57. How can you handle when one microservice calls another microservice and that microservice takes more time to respond?

There are many approaches to resolve this issue but we will stick to standard approaches.

The first approach is that we can go for circuit breaker pattern and use a circuit breaker framework like resilience4j and return a fallback response to the calling microservice.

Another approach is that we can configure a Timeout class that will wait for a particular time to get the response and after that, it may or may not return a fallback response.

58. Can you give an example of a Builder design pattern related to spring security?

In the WebSecurityConfigurerAdapter the protected void configure(HttpSecurity http) is a very good example of the Builder design pattern.

59. How do spring boot archives convention over configurations?

In spring boot we have AutoConfiguration which scans our classpath for the requirement and configures our application automatically according to the requirement. For example, if we add spring-boot-starter-web dependency then Spring boot understands that we are going to make a web application then it configures a Tomcat server, web dispatcher, etc automatically for us.

Another convention over configuration is Dependency Injection in spring. In the old approach, we will need to have an XML file where we specify the dependencies that need to be injected but in the new approach we need to use the @Autowired annotation and the dependency can be easily injected without doing many configurations.

Another one is the application.properties file we can just specify the database name, URL, and password in the file, and spring boot will auto configure the database connection for us.

60. What will be the output of the below code?(Type Promotion)

public class Main{
public static void main(String[] args) {
add(2,3);
}

public static void add(int a, long b){
System.out.println( a + b);
}
}

Output:

5

Here the Integer 3 will be type promoted to long 3 and the add method will be called and we receive the output as 5.

Now let's take look at another situation.

public class Main{
public static void main(String[] args) {
add(2,3);
}

public static void add(int a, long b){
System.out.println( a + b);
}
public static void add(long a, int b){
System.out.println( a + b);
}
}

Now the above code will throw a compilation error because of the ambiguous situation the compiler is not able to figure out which add method need to be called.

Let's take one more example to understand things better.

public class Main{

public static void main(String[] args) {
add(2,3);
}

public static void add(int a, long b){
System.out.println( a + b);
}
public static void add(long a, int b){
System.out.println( a + b);
}

public static void add(int a, int b){
System.out.println( a + b);
}
}

Output:

5

Now there is no need for type promotion as there is a direct method of add(int a, int b) will be called and we will get 5 as output.

Stay tuned for more such questions.

--

--

Abhijit Jadhav

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