Top 40 Most Frequently Asked Spring Boot Interview Questions and Answers

In this tutorial, I will be sharing 40 Spring Boot interview questions and answers for both beginners and experienced professionals. If you like the article please bookmark it. I will keep adding questions to this post with future releases of Spring Boot.

Read Also:  Best Spring Boot Online Course for Beginners

Top 40 Spring Boot Interview Questions and Answers

Q1 What is Spring Boot?

Spring Boot is an opensource Java-based framework that is built on top of the Spring framework.
It is a framework that makes it easy to create production-grade, standalone Spring-based applications.

It is developed by Pivotal Software.

Q2 What do you understand by the term "Opinionated" in the context of Spring Boot?

Since Spring Boot auto-configure many dependencies that's why it is opinionated. It is Spring Boot that decides for you which defaults to use for the configuration. As per requirements, you can also override auto-configuration settings in Spring Boot.

Q3 What are the features of the Spring Boot?

The main features of the Spring Boot are as follow:


a. Spring Application
b. Externalized Configuration
c. Profiles
d. Logging
e. Internationalization
f. Developing Web Applications
g. Security 

You can find it in detail here.

Q4 What are the goals or advantages of the Spring Boot?

The main goals or advantages of the Spring Boot are :

a. Support RAD (rapid application development).
b. Be opinionated out of the box but you can customize it as per your requirements.
c. It provides many non-functional features such as embedded servers (tomcat, jetty, undertow), health checks, security, metrics, etc.

Q5 Name the few embedded servlet containers of Spring Boot?

The embedded servlet containers of Spring Boot are:

a. Tomcat
b. Jetty
c. Undertow

Q6 What are the build tools? Which build tools are recommended by Spring Boot?

According to Wikipedia,
Build automation tools are tools that involve automating the process of compiling computer source code into binary code.

Spring Boot recommends build tools that support dependency management. Mainly, two build tools are frequently used in Spring Boot applications:
a. Maven
b. Gradle

Q7 What is the role of @SpringBootApplication annotation?

@SpringBootApplication is equivalent to the combination of three annotations i.e @EnableAutoConfiguration, @ComponentScan and @Configuration.

You can find in detail about all the three annotations here.

Q8 What is spring-boot-devtools in Spring Boot?

Whenever files on the classpath change then application using spring-boot-devtools restart automatically.

Q9 How does the spring-boot-devtools provided auto-restart work in the Spring Boot?

The restart technology provided by Spring Boot depends upon two classloaders i.e base classloader and restart classloader.

Base classloader: This classloader loads the classes that do not change. For example third party jars.

Restart classloader: This classloader loads the classes that you are actively developing.

The restart classloader is thrown away when the application is restarted. Meanwhile, a new restart classloader is created. This process makes the application restarts much faster than "cold starts" since the base classloader is already present and populated. 

Q10 What is the role of @RestController annotation in Spring Boot?

The @RestController annotation is also known as a stereotype annotation. It provides a hint to Spring that the class plays a specific role.

It is a combination of @ResponseBody and @Controller annotations.

Q11 What is lazy initialization in Spring Boot?

When the lazy initialization is enabled in Spring Boot, the beans are created as they are needed rather than during application startup. As a result, the application starts much faster.

Q12 Why the lazy initialization is disabled by default for Spring Boot applications even though it helps in reducing the application startup time?

There are a couple of issues arise if the lazy initialization is enabled by default:

a. Delay the discovery of problem: If a bean is misconfigured than failure will no longer occur during application startup time and the problem will only become apparent when the bean is initialized.

b. JVM memory allocation: Make sure JVM has enough memory to accommodate all of the application's beans and not just for those beans that are initialized during the startup of the application. That's why it is recommended that fine-tuning of JVM's heap size is done before enabling lazy initialization.

Q13 What are profiles in Spring Boot?

Profiles is a feature of Spring Boot. Segregate parts of application configuration and make it available for specific environments(DEV, IT, UAT, PROD, etc.) that can be achieved through Spring Profiles.

Example: The below class ProductionConfiguration will be available only in the production environment.

@Configuration
@Profile("production")
public class ProductionConfiguration {

  // ...

}


Q14 What is the name of the configuration file that you use in the Spring Boot? 

application.properties is the configuration file that you use in the Spring Boot application.

Q15 What is Spring Boot Actuator?

a. Spring Boot Actuator helps in monitoring and managing your application when you push it to production.

b. By using HTTP endpoints you can manage and monitor your application.

Q16 How do you change the default port of embedded server in Spring Boot?

There are many ways we can change the default port of embedded server in Spring Boot. One of the ways is by changing server.port  property value in the application.properties file.

You can find the answer in detail here.

Q17 What is Spring Boot Starter?

a. Spring Boot provides a number of starters which are also called dependency descriptors that you can include in your application.

b. Spring Boot starters accommodate all the Spring and related technologies without having to go through sample code and copy-paste loads of dependency descriptors.

c. All the starters in the Spring Boot framework follow a similar naming pattern:
spring-boot-starter-*  where * indicates an application.

If you want database access using Spring and JPA, then include the spring-boot-starter-data-jpa dependency in your project.

If you want to create a web application using RESTful web services, then include spring-boot-starter-web in your project.

Q18 What is the default logging used in Spring Boot if you use the "Starters" in your application?

Logback is used for logging if you use the "Starters" in your application.

Q19 How will you disable specific auto-configuration in Spring Boot?

a. At annotation level: exclude property is used to disable specific auto-configuration classes in Spring Boot.

In the below example we are excluding DataSourceAutoConfiguration class:

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

b. At property level: You can also use the spring.autoconfigure.exclude property to exclude certain auto-configuration classes.

Q20 What is Thymeleaf?

Thymeleaf is a java based server-side template engine for the web application. The main advantage of Thymeleaf is to bring stylish natural templates to your web application.

Q21 What dependency is required to use Thymeleaf in the Spring Boot application?

Below dependency must be added in your application to use Thymeleaf.

<dependency>    
     <groupId>org.springframework.boot</groupId>    
     <artifactId>spring-boot-starter-thymeleaf</artifactId>    
</dependency>


Q22 What is the role of @RequestMapping annotation in Spring Boot?

Web requests are mapped by the @RequestMapping annotation. In other words, it provides routing information.

Q23 What are the minimum requirements of the Spring Boot 2 System?

Spring Boot 2.2.2.RELEASE requires:

Java: Minimum Java 8. It is compatible upto Java 13.
Spring Framework:  5.2.2.RELEASE and above.

Build Tools:
Maven: 3.3 and above.
Gradle: 5.x and 6.x

Embedded servlet containers:
Tomcat: 9.0
Jetty: 9.4
Undertow: 2.0

Servlet version:
You can deploy Spring Boot applications to any servlet 3.1 and above the compatible container.

Q24 How to create a project using Spring Boot Initializr?

You can find a complete step by step process to create a project using Spring Boot Initializr here.

Q25 How you can enable HTTP/2 support in Spring Boot?

It is a one-liner: Just add the following line in application.properties
server.http2.enabled=true

Q26 Which is the only endpoint disabled by default in Spring Boot Actuator? How do you enable it?

shutdown is the only endpoint that is disabled by default in Spring Boot Actuator.

You can enable the shutdown endpoint by using the following property:
management.endpoint.shutdown.enabled=true

Q27 What is shutdown in Spring Boot Actuator?

shutdown is an endpoint in Spring Boot Actuator that is used to gracefully shutdown the application.

Q28 How do you configure Log4j2 for Logging in Spring Boot?

If you are using "Starters" in your Spring Boot project, then, you have to exclude Logback and include Log4j2.

Q29 What is the default embedded servlet container for web application in Spring Boot?

Tomcat is the default embedded servlet container for web application in Spring Boot

Q30 How to use jetty instead of tomcat while using spring-boot-starter-web in Spring Boot project?

You can use the jetty embedded servlet container instead of tomcat by excluding the spring-boot-starter-tomcat dependency from spring-boot-starter-web.

<dependency>    
     <groupId>org.springframework.boot</groupId>    
     <artifactId>spring-boot-starter-web</artifactId>
     <exclusions>
          <exclusion>
               <groupId>org.springframework.boot</groupId>    
               <artifactId>spring-boot-starter-tomcat</artifactId>
          </exclusion>
     </exclusions>    
</dependency>

You also need to add the following in pom.xml

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

Q31 How you can create an executable jar in Spring Boot?

You can create an executable jar in Spring Boot by adding spring-boot-maven-plugin in pom.xml as shown below:

<build>    
     <plugins>
          <plugin>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-maven-plugin</artifactId>    
          </plugin>
     </plugins>
</build> 

Q32 How to use a random unassigned port in Spring Boot?

You can  the random unassigned port for the Spring Boot application by using the property server.port=0

Q33 Can you run Spring Boot application packaged as executable war using java -jar command?

Yes, you can use java -jar command to run the Spring Boot application packaged as executable war.

Q34 What is LiveReload in Spring Boot?

spring-boot-devtools module contains an embedded LiveReload server. It can be used to trigger a browser refresh when a resource is changed. At a time, you can only run one LiveReload server. 

Q35 What is the difference between Spring and Spring Boot?

Spring is a java based web application framework. You can create customized web applications by using tools and libraries provided by the Spring framework.

Spring Boot is a module of Spring that is used to create production-grade and standalone Spring-based applications.

Q36 What are the different ways to externalize configurations in Spring Boot?

There are 4 ways to externalize configurations in Spring Boot.

a. Using properties files
b. Using YAML files
c. Environment variables
d. Command line arguments

Q37 How do you write a JSON REST web service in Spring Boot?

If @RestController is present in the Spring Boot application, then, it will render the response by default in JSON format as long as  Jackson2 is on the classpath.

Q38 What are the disadvantages of Spring Boot?

a. Spring Boot can contain dependencies that are not in use thus causing the huge deployment file size.
b. Converting legacy Spring applications into Spring Boot application requires a significant effort and a time-consuming process.

Q39 Using Maven plugin can you run Spring Boot application?

Yes, by using the below command we can run Spring Boot application using Maven plugin.

mvn spring-boot:run


Q40 What is the difference between CommandLineRunner and ApplicationRunner in Spring Boot?

CommandLineRunner and ApplicationRunner both are interfaces. Both works in the same way and provides a single run() method.

The difference between CommandLineRunner and ApplicationRunner is given below:

CommandLineRunner gives access to the application arguments as a simple String[] array where as ApplicationRunner uses the ApplicationArguments interface that provides access to both raw String[] arguments as well as parsed non-option and option arguments.

That's all for today, please mention in comments in case you have any questions related to Spring Boot interview questions and answers for beginners and experienced professionals.

References:
Spring Boot Doc


About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry