Spring Boot has dramatically simplified how developers create and run Spring applications. It allows for bootstrapping a Spring application without the traditional configuration setup. But how well do you understand its basics? Here’s a beginner-level quiz focused on the fundamental concepts of Spring Boot.
Note that each question is followed by the correct answer and an explanation to help reinforce your knowledge.
1. What is the primary advantage of Spring Boot?
Answer:
Explanation:
Spring Boot aims to provide a way to simplify the setup and development of Spring applications. By offering default configurations, an embedded server, and a host of other features, it helps developers create production-ready applications with minimal setup and boilerplate code.
2. What does the @SpringBootApplication annotation do?
Answer:
Explanation:
This convenience annotation combines other annotations to bootstrap a Spring Boot application with sensible defaults.
3. How can you change the default port (8080) for a Spring Boot application?
Answer:
Explanation:
By default, Spring Boot applications run on port 8080. This can be changed by setting the server.port property in the application.properties file.
4. What does Spring Boot’s “starter” dependency provide?
Answer:
Explanation:
Starters are a set of dependency descriptors that can be included in the application. They simplify Maven configuration by providing everything you need to get started with a particular functionality.
5. How does Spring Boot provide database connectivity?
Answer:
Explanation:
Spring Boot can configure a lot out-of-the-box. It can set up a connection via direct JDBC, use Spring Data for data access, or integrate with Hibernate for ORM.
6. What is the file name convention for configuration properties in Spring Boot?
Answer:
Explanation:
The default properties file in Spring Boot is application.properties. However, application.yml is another option for YAML based configurations.
7. How can you create a Spring Boot project using a web interface?
Answer:
Explanation:
Spring Initializr is a web-based tool provided by Spring to bootstrap a new Spring Boot application.
8. Which embedded servlet container does Spring Boot support out of the box?
Answer:
Explanation:
Spring Boot has built-in support for Tomcat, Jetty, and Undertow. Tomcat is the default embedded server.
9. In Spring Boot, where can you define profile-specific configuration properties?
Answer:
Explanation:
Profile-specific properties can be defined in files named application-{profile}.properties where {profile} is the name of the profile.
10. Which Spring Boot Starter would you use for developing web applications?
Answer:
Explanation:
spring-boot-starter-web is used for building web, including RESTful, applications using Spring MVC.
11. Which annotation is used to inject configuration properties into a bean?
Answer:
Explanation:
The @ConfigurationProperties annotation is used to map property values from application.properties or application.yml to a bean.
12. Which file is automatically picked up by Spring Boot for logging configuration if present?
Answer:
Explanation:
By default, Spring Boot uses Logback for logging and it automatically configures it using the logback.xml if present.
13. What is the use of @EnableAutoConfiguration in Spring Boot?
Answer:
Explanation:
@EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
14. Which of the following files can be used to customize the banner in a Spring Boot application?
Answer:
Explanation:
Spring Boot provides the ability to customize the application banner using either a banner.txt or a banner.html file.
15. Which starter dependency is used to build standalone Spring Boot applications that can be started using Java -jar?
Answer:
Explanation:
spring-boot-starter-web provides all the necessary dependencies and configurations to build a web application that can be started using Java -jar.
16. What is the use of spring-boot-starter-parent?
Answer:
Explanation:
spring-boot-starter-parent is a parent POM providing dependency and plugin management for applications built with Maven.
17. How can you include Spring Boot DevTools into your project?
Answer:
Explanation:
By simply adding the spring-boot-devtools module dependency, you can get developer-specific features like automatic restart and live reload.
18. In Spring Boot, how can you specify profile-specific properties?
Answer:
Explanation:
Spring Boot provides multiple ways to set active profiles. You can use the –spring.profiles.active command-line argument or set it in application.properties.
19. Which Spring Boot Starter is used to include JDBC support?
Answer:
Explanation:
To add basic JDBC support (with HikariCP as default connection pool), you use the spring-boot-starter-jdbc starter.
20. Which annotation is used to make a Java class serve RESTful requests?
Answer:
Explanation:
@RestController is a specialized version of the controller that includes @Controller and @ResponseBody annotations, making it suited for building RESTful APIs.
21. Which of the following dependencies will include an embedded Tomcat server in a Spring Boot project?
Answer:
Explanation:
Both spring-boot-starter-web and spring-boot-starter-tomcat will include an embedded Tomcat server. The former is used for web applications and the latter for Tomcat specifically.
22. How can you exclude a specific auto-configuration in Spring Boot?
Answer:
Explanation:
The exclude attribute of the @SpringBootApplication annotation allows you to exclude specific auto-configuration classes from being applied.
23. What does the spring-boot-maven-plugin provide?
Answer:
Explanation:
The spring-boot-maven-plugin provides the capability to build an executable JAR or WAR which bundles the Tomcat, Jetty, or Undertow servlet container.
24. How can you specify the active profile while running a Spring Boot application as a JAR?
Answer:
Explanation:
You can specify the active profile using -Dspring.profiles.active=profileName when running a Spring Boot application as a JAR.
25. In a Spring Boot application, which annotation ensures that HTTP methods like GET and POST are mapped to specific controller methods?
Answer:
Explanation:
The @RequestMapping annotation ensures that HTTP requests are mapped to specific controller methods.
26. In Spring Boot, which annotation is used to schedule a method to be run at fixed intervals?
Answer:
Explanation:
The @Scheduled annotation is used to execute a method periodically using fixed-rate or fixed-delay intervals.
27. Which Spring Boot starter is used for building web applications, including RESTful APIs?
Answer:
Explanation:
spring-boot-starter-web is used for building web applications, including RESTful APIs.
28. How do you change the default port (8080) on which a Spring Boot web application runs?
Answer:
Explanation:
The default port can be changed using the server.port property in application.properties.
29. Which annotation is used to define a JPA repository in Spring Boot?
Answer:
Explanation:
While JPA repositories extend the JpaRepository interface, the actual beans in Spring are usually annotated with @Repository.
30. What is the use of the @Profile annotation in Spring Boot?
Answer:
Explanation:
The @Profile annotation is used to indicate that a component or configuration is specific to a profile.
31. In Spring Boot, which annotation is used to roll back a transaction in case of an exception?
Answer:
Explanation:
The @Transactional annotation ensures that a method is executed within a transactional context and, by default, it rolls back the transaction if a runtime exception is thrown.
32. In Spring Boot, how do you expose actuator endpoints over HTTP?
Answer:
Explanation:
The actuator provides production-ready features for Spring Boot apps. To expose these endpoints over HTTP, you need to include the spring-boot-starter-actuator dependency.
33. How can you secure sensitive actuator endpoints in Spring Boot?
Answer:
Explanation:
Spring Security provides a way to secure actuator endpoints. Once Spring Security is in the classpath, the actuator endpoints are secured by default. Further customization can be done using Spring Security configuration.