Hi Friends,
In this post, I'm sharing Technical Architect interview round questions asked in Tech Mahindra.
Kindly also go through my other interview posts:
Question 1:
What are Spring boot actuator endpoints?
Answer 1:
Spring boot actuator is used to monitor and manage application usage in production environment without coding and configuration for any of them.
This monitoring and managing information is exposed via REST like endpoint URL's.
Actuator endpoints:
/env : Returns list of properties of current environment.
/health : Returns application health information.
/beans : Returns a complete list of all the spring beans in the application.
/trace : Returns trace logs [By default last 100 HTTP requests]
/dump : It performs a thread dump.
/metrics : It shows several useful metrics information like JVM memory usage, CPU usage, open files and much more.
Note: These endpoints can be explicitly enabled/disabled.
If we need to list all endpoints in the browser:
Just run the application and from the browser, use localhost:8080/actuator. It will list 2 endpoints by default : info and health.
For other endpoints, we need to expose them manually.
Now, if we want to explore health endpoint, then just open localhost:8080/actuator/health URL and it will just display "status" : "UP"
We can enable or disable the endpoint from application.properties file:
management.endpoint.shutdown.enabled = true
By default , all the endpoints are enabled except shutdown endpoint.
Question 2:
What is the difference between map and flatmap in java 8?
Answer 2:
map() method is used to map an object or entry from stream to some other value.
flatMap() method on the other hand applies map() on the entries and then flatten the result.
e.g.:
Suppose we have a string array with entries:
String[] strArray = {"12", "46"};
And we have to find all permutations of these strings.
So output with map() method will be: [12, 21] , [46, 64]
While the output with flatMap() method will be : [12, 21, 46, 64]
Lets take another example:
List<Integer> evens = Arrays.asList(2,4,6,8);
List<Integer> odds = Arrays.asList(1,3,5,7);
List<Integer> primes = Arrays.asList(5,7,11,13);
List numbers = Stream.of(evens, odds, primes).flatMap(list-> list.stream()).collect(Collectors.toList());
System.out.println("List = "+numbers);
Note: this output is possible only with flatMap() method.
Question 3:
Explain Spring Data JPA versus Hibernate?
Answer 3:
JPA : Java Persistence API provides specification for creation, deletion, persistence and data management from java objects to relations [tables] in database.
Hibernate : There are various providers which implement JPA. Hibernate is one of them. So , we have other providers as well e.g.: Eclipse Link.
Spring Data JPA: This is another layer on top of JPA which spring provides to make coding easier.
Question 4:
When to use @Component and when to use @Bean?
Answer 4:
@Component is a class level annotation while @Bean is a method level annotation.
@Component is preferable for component scanning and automatic wiring.
Sometimes automatic configuratin is not an option. When? Let's imagine that we want to wire components from 3rd party libraries[where we don't have the source code ,so we can't annotate the class with @Component ], so automatic configuration is not possible.
So, in this case , we should use @Bean.
The @Bean annotation returns an object that spring should register as bean in application context/Spring IOC container. The body of the method bears the logic responsible for creating the instance.
Another difference is that:
@Component does not decouple the declaration of bean from the class definition whereas @Bean decouples the declaration of the bean from the class definition.
There are two ways to create beans:
One is to create a class with an annotation @Component. The other is to create a method and annotate it with @Bean.
@Bean requires the @Configuration annotation on the class in which @Bean is used. Whereas @Component doesn't require the class to be annotated with @Configuration.
Once we run the spring project, the class with @ComponentScan annotation would scan every class with @Component on it and store the instance of this class to the IOC container.
Another thing , the @ComponentScan would do is , running the methods with @Bean on it and store the returned object to the IOC container as a bean.
Question 5:
Explain the use case with example where @Bean is more beneficial to use than @Component?
Answer 5:
Lets suppose, we want some specific implementation depending on some dynamic state. @Bean is perfect for that case.
@Bean
@Scope("prototype")
public Service anyService(){
switch(state){
case 1:
return new Impl1();
case 2:
return new Impl2();
case 3:
return new Impl3();
default:
return new Impl();
}
}
In the above case, only @Bean can be used , not the @Component.
Note: We can use both @Component and @Configuration annotations in our same application/module.
Question 6:
Explain the difference between @Service and @Component in Spring.
Answer 6:
@Component is a generic for other stereotypes.
So, we can replace @Component with @Service, @Repository or @Controller and nothing will change. But for better readability , we should use @Repository, @Service and @Controller.
They all inform spring that the class is involved in the DI context.
In order to configure spring so that it can provide us with the instances of the class we need , we are supposed to tell spring what objects are involved and how they are built. To do this, we can use XML configuration file or java annotations.
They also have semantic meaning:
That's all for this post.
Hope this post helps everybody in their interviews.
Thanks for reading !!
In this post, I'm sharing Technical Architect interview round questions asked in Tech Mahindra.
Kindly also go through my other interview posts:
- Microservices interview 4 : circuit breaker
- Interview @ Sirion Labs Round - 2
- Interview @ Sirion Labs Round - 1
- Java 8 interview Questions - Part - I
- Java Interview @ Gemini Solutions
Question 1:
What are Spring boot actuator endpoints?
Answer 1:
Spring boot actuator is used to monitor and manage application usage in production environment without coding and configuration for any of them.
This monitoring and managing information is exposed via REST like endpoint URL's.
Actuator endpoints:
/env : Returns list of properties of current environment.
/health : Returns application health information.
/beans : Returns a complete list of all the spring beans in the application.
/trace : Returns trace logs [By default last 100 HTTP requests]
/dump : It performs a thread dump.
/metrics : It shows several useful metrics information like JVM memory usage, CPU usage, open files and much more.
Note: These endpoints can be explicitly enabled/disabled.
If we need to list all endpoints in the browser:
Just run the application and from the browser, use localhost:8080/actuator. It will list 2 endpoints by default : info and health.
For other endpoints, we need to expose them manually.
Now, if we want to explore health endpoint, then just open localhost:8080/actuator/health URL and it will just display "status" : "UP"
We can enable or disable the endpoint from application.properties file:
management.endpoint.shutdown.enabled = true
By default , all the endpoints are enabled except shutdown endpoint.
Question 2:
What is the difference between map and flatmap in java 8?
Answer 2:
map() method is used to map an object or entry from stream to some other value.
flatMap() method on the other hand applies map() on the entries and then flatten the result.
e.g.:
Suppose we have a string array with entries:
String[] strArray = {"12", "46"};
And we have to find all permutations of these strings.
So output with map() method will be: [12, 21] , [46, 64]
While the output with flatMap() method will be : [12, 21, 46, 64]
Lets take another example:
List<Integer> evens = Arrays.asList(2,4,6,8);
List<Integer> odds = Arrays.asList(1,3,5,7);
List<Integer> primes = Arrays.asList(5,7,11,13);
List numbers = Stream.of(evens, odds, primes).flatMap(list-> list.stream()).collect(Collectors.toList());
System.out.println("List = "+numbers);
Note: this output is possible only with flatMap() method.
Question 3:
Explain Spring Data JPA versus Hibernate?
Answer 3:
JPA : Java Persistence API provides specification for creation, deletion, persistence and data management from java objects to relations [tables] in database.
Hibernate : There are various providers which implement JPA. Hibernate is one of them. So , we have other providers as well e.g.: Eclipse Link.
Spring Data JPA: This is another layer on top of JPA which spring provides to make coding easier.
Question 4:
When to use @Component and when to use @Bean?
Answer 4:
@Component is a class level annotation while @Bean is a method level annotation.
@Component is preferable for component scanning and automatic wiring.
Sometimes automatic configuratin is not an option. When? Let's imagine that we want to wire components from 3rd party libraries[where we don't have the source code ,so we can't annotate the class with @Component ], so automatic configuration is not possible.
So, in this case , we should use @Bean.
The @Bean annotation returns an object that spring should register as bean in application context/Spring IOC container. The body of the method bears the logic responsible for creating the instance.
Another difference is that:
@Component does not decouple the declaration of bean from the class definition whereas @Bean decouples the declaration of the bean from the class definition.
There are two ways to create beans:
One is to create a class with an annotation @Component. The other is to create a method and annotate it with @Bean.
@Bean requires the @Configuration annotation on the class in which @Bean is used. Whereas @Component doesn't require the class to be annotated with @Configuration.
Once we run the spring project, the class with @ComponentScan annotation would scan every class with @Component on it and store the instance of this class to the IOC container.
Another thing , the @ComponentScan would do is , running the methods with @Bean on it and store the returned object to the IOC container as a bean.
Question 5:
Explain the use case with example where @Bean is more beneficial to use than @Component?
Answer 5:
Lets suppose, we want some specific implementation depending on some dynamic state. @Bean is perfect for that case.
@Bean
@Scope("prototype")
public Service anyService(){
switch(state){
case 1:
return new Impl1();
case 2:
return new Impl2();
case 3:
return new Impl3();
default:
return new Impl();
}
}
In the above case, only @Bean can be used , not the @Component.
Note: We can use both @Component and @Configuration annotations in our same application/module.
Question 6:
Explain the difference between @Service and @Component in Spring.
Answer 6:
@Component is a generic for other stereotypes.
So, we can replace @Component with @Service, @Repository or @Controller and nothing will change. But for better readability , we should use @Repository, @Service and @Controller.
They all inform spring that the class is involved in the DI context.
In order to configure spring so that it can provide us with the instances of the class we need , we are supposed to tell spring what objects are involved and how they are built. To do this, we can use XML configuration file or java annotations.
They also have semantic meaning:
- @Controller : @Component belonging to presentation layer
- @Service : @Component belonging to Service/Use case layer
- @Repository : @Component belonging to Persistence layer
That's all for this post.
Hope this post helps everybody in their interviews.
Thanks for reading !!