Last Updated On
Welcome to the SpringBoot Quiz! This article delves into the essential concepts and features of Spring Boot. It covers annotations, configuration properties, and metrics. The article also discusses caching mechanisms and shares best practices for building robust applications.

1. Which of the following is true about Spring Boot?
a) Spring Boot is a separate framework and not a part of the Spring ecosystem
b) Spring Boot requires manual configuration for every aspect of the application
c) Spring Boot simplifies the development of Spring applications by providing an opinionated approach and auto-configuration
d) Spring Boot is only suitable for small-scale applications with limited functionality
Answer 1
c) Spring Boot simplifies the development of Spring applications by providing an opinionated approach and auto-configuration
Spring Boot simplifies the development of new Spring applications through convention over configuration and automatic setup. Spring Boot eliminates the need for manual configuration. It provides sensible defaults. It also offers automatic configuration based on classpath scanning and dependency management.
2. What is the purpose of the @ConfigurationProperties annotation?
@ConfigurationProperties(prefix = "app")
public class AppConfig {
private String name;
private String version;
// Getters and setters
}
a) It defines a bean with a specific name.
b) It maps properties to a class.
c) It creates a REST controller.
d) It handles exceptions.
Answer 2
b) It maps properties to a class.
The @ConfigurationProperties annotation is used to bind external properties (e.g., those defined in application.properties or application.yml) to fields in a Java class. In the example provided, properties with keys like “app.name” and “app.version” would be mapped to the fields `name` and `version` in the `AppConfig` class.
3. Which of the following statements is true about the @Autowired annotation in Spring Boot?
a) @Autowired is used to inject dependencies into Spring Boot applications
b) @Autowired is only applicable for injecting dependencies of type String
c) @Autowired is optional and not required for dependency injection in Spring Boot
d) @Autowired is mandatory in Spring Boot and can only be used in service classes
Answer 3
a) @Autowired is used to inject dependencies into Spring Boot applications
Autowired is used to inject dependencies into Spring Boot applications. With Autowired annotation, dependencies can be automatically injected into the dependent class by Springs dependency injection mechanism.
4. What does the @Autowired annotation do in the following code?
@Service
public class ArticleService {
@Autowired
private ArticleRepository articleRepository;
}
a) It creates a new instance of ArticleRepository.
b) It injects an existing instance of ArticleRepository
c) It defines a REST endpoint
d) It marks the class as a Spring component
Answer 4
b) It injects an existing instance of ArticleRepository
The @Autowired annotation is used to inject an existing instance of the ArticleRepository into the ArticleService
5. Which annotation is used to automatically configure a Spring Boot application for RESTful services?
a) @RestController
b) @Service
c) @Repository
d) @Component
Answer 5
a) @RestController
The @RestController annotation is used to define a controller that handles RESTful web service requests.
6. Which of the following code snippets demonstrates the correct configuration of a Spring Boot REST Controller?
a)
@RestController
public class MyController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
b)
@Controller
@RequestMapping("/api")
public class MyController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
c)
@RestController
@RequestMapping("/api")
public class MyController {
@RequestMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
d)
@Controller
public class MyController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
@ResponseBody
public String hello() {
return "Hello, World!";
}
}
Answer 6
Option a) has the correct configuration for a Spring Boot REST Controller utilizing the simplified annotations that Spring provides
Options b) and d) use @Controller which would require @ResponseBody for RESTful outputs if not using @RestController. Option c) incorrectly utilizes @RequestMapping instead of @GetMapping for the GET request.
7. Which of the following code snippets demonstrates the correct configuration of a Spring Boot application class?
a)
@Configuration
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
b)
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
c)
@RestController
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
d)
@Component
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
Answer 7
Option b) demonstrates the correct configuration of a Spring Boot application class. The @SpringBootApplication annotation is used to mark the class as the main application class in Spring Boot.
8. What is the purpose of the @Cacheable annotation?
@Cacheable("articles")
public Article getArticleById(Long id) {
return articleRepository.findById(id).orElse(null);
}
a) It stores the result in the database
b) It caches the method’s result
c) It logs the method’s execution time
d) It defines a scheduled task
Answer 8
b) It caches the method’s result
The @Cacheable annotation caches the result of the method call, so subsequent calls with the same parameters can return the cached result.
9. In the context of Spring Boot, what does @SpringBootTest annotation do?
a) It is used to run unit tests only
b) It loads the complete application context for integration tests
c) It defines a REST endpoint for testing
d) It initializes application properties
Answer 9
b) It loads the complete application context for integration tests
The @SpringBootTest annotation is used to create an application context for integration tests, allowing the entire application to be tested as a whole
10. What type of object is returned by a Spring Boot REST endpoint when using ResponseEntity?
a) A plain Java object
b) A JSON string
c) An HTTP response with status and body
d) A configuration property
Answer 10
c) An HTTP response with status and body
The ResponseEntity class represents an HTTP response, including status code, headers, and body
11. How do you specify an endpoint that accepts JSON data in a POST request?
@PostMapping("/articles")
public ResponseEntity<Article> createArticle(@RequestBody Article article) {
// Save article logic
return ResponseEntity.status(HttpStatus.CREATED).body(savedArticle);
}
Choose one option
a) The endpoint only accepts URL-encoded data
b) The method uses @RequestBody to map the request body to an Article object
c) The endpoint does not return any response
d) The method automatically handles validation
Answer 11
b) The method uses @RequestBody to map the request body to an Article object
The @RequestBody annotation binds the JSON data from the request body to the Article object
12. How do you disable a specific auto-configuration class in Spring Boot?
Choose one option
a) Set auto-configuration.enabled: false for the class
b) Use the @DisableAutoConfiguration annotation
c) Manually exclude the class from the application context
d) Use the exclude attribute of @SpringBootApplication annotation
Answer 12
d) Use the exclude attribute of @SpringBootApplication annotation
The exclude attribute allows specifying auto-configuration classes to be excluded from the auto-configuration process.
13. What does the @PathVariable annotation do in a Spring Boot REST controller?
Choose one option
a) Maps the URL path to a specific request method
b) Extracts values from the URL path placeholder
c) Links to external web resources
d) Automatically authenticates the request
Answer 13
b) Extracts values from the URL path placeholder
The @PathVariable annotation can be used to extract values from the URI template so that the method can use those values
14. How do you specify active profiles in a Spring Boot application?
Choose one option
a) By editing the maven settings.xml
b) By specifying profiles in application.properties as spring.profiles.active
c) By setting the profile configuration in web.xml
d) Spring Boot does not support active profiles
Answer 14
b) By specifying profiles in application.properties as spring.profiles.active
Active profiles can be specified with the spring.profiles.active property, allowing for environment-specific configurations.
15. Which of the following code snippets demonstrates the correct configuration of Spring Boot Security for securing a REST API?
Choose one option
a)
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/api/**").hasRole("ADMIN")
.anyRequest().permitAll()
.and()
.httpBasic();
}
}
b)
@SpringBootApplication
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/api/**").authenticated()
.anyRequest().permitAll()
.and()
.httpBasic();
}
}
c)
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/api/**").authenticated()
.anyRequest().permitAll()
.and()
.httpBasic()
.and()
.csrf().disable();
d)
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/api/**").permitAll()
.anyRequest().authenticated()
.and()
.httpBasic();
}
}
Answer 15
Option c) demonstrates the correct configuration of Spring Boot Security for securing a REST API. The code snippet uses the @Configuration annotation to mark the class as a Spring configuration. The @EnableWebSecurity annotation enables Spring Security features. The configure(HttpSecurity http) method is overridden to define the security rules. In this case, /api/** endpoint is restricted to authenticated users only while allowing public access to all other endpoints. The .httpBasic() method configures basic authentication for the REST API.
16. What is the result of the following code snippet?
@Bean
@ConditionalOnProperty(name = "app.feature.enabled", havingValue = "true")
public MyFeature myFeature() {
return new MyFeature();
}
Choose one option
a) The bean is always created
b) The bean is created only if the property is set to true
c) The bean is not created at all
d) The property must be set to false for the bean to be created
Answer 16
b) The bean is created only if the property is set to true
The @ConditionalOnProperty annotation creates the bean only if the specified property is set to true.
17. In Spring Boot, what does the spring.jpa.hibernate.ddl-auto property control?
Choose one option
a) The caching mechanism
b) The logging level of Hibernate
c) The automatic schema generation strategy
d) The transaction management settings
Answer 17
c) The automatic schema generation strategy
The spring.jpa.hibernate.ddl-auto property controls how Hibernate handles schema generation, such as create, update, or none.
18. Which of the following statements is true about Spring Database integration?
a) Spring provides a built-in database management system for relational databases
b) Spring only supports integration with SQL Server and Oracle databases
c) Spring provides support for accessing and interacting with databases through JDBC and Object-Relational Mapping (ORM) frameworks
d) Spring Database integration is limited to NoSQL databases only
Answer 18
c) Spring provides support for accessing and interacting with databases through JDBC and Object-Relational Mapping (ORM) frameworks
Spring Framework, along with Spring Boot, offers comprehensive support for database access, including integration with JDBC and various ORM frameworks like Hibernate and JPA. This support extends across a wide range of relational and non-relational databases, facilitating easy configuration, transaction management, and data access within applications.
19. Which of the following is true about Spring Security?
a) Spring Security is a framework that provides authentication and authorization support for Spring-based applications
b) Spring Security is only applicable for securing web applications and does not support other types of applications
c) Spring Security is a stand-alone framework and cannot be integrated with other Spring modules
d) Spring Security does not provide any out-of-the-box support for implementing multi-factor authentication
Answer 19
a) Spring Security is a framework that provides authentication and authorization support for Spring-based applications
It allows developers to easily secure their applications against common security vulnerabilities. It also has support for various authentication mechanisms, such as form-based, HTTP Basic, and OAuth.
20. How can you define a scheduled task in a Spring Boot application?
@Scheduled(fixedRate = 2000)
public void reportCurrentTime() {
System.out.println("Current time: " + new Date());
}
Choose one option
a) It runs every 2 seconds
b) It is triggered by an event
c) It executes only once
d) It runs at application startup only
Answer 20
a) It runs every 2 seconds
21. Which of the following layers is not part of a typical Spring Boot application?
a) DAO layer
b) Logger layer
c) Service layer
d) Controller layer
Answer 21
b) Logger layer
Common layers in a Spring Boot application include the DAO, Service, and Controller layers; logging is a cross-cutting concern, not a separate layer.
22. What is the default package used in a Spring Boot project if none is specified?
a) main.java
b) default.package
c) org.springframework.boot
d) The root package specified in the @SpringBootApplication class
Answer 22
d) The root package specified in the @SpringBootApplication class
The @SpringBootApplication annotation triggers auto-configuration only within its package and sub-packages, so it acts as the root package if not specified.
23. How can you disable the default web server in a Spring Boot application in build process?
a) Set server.enabled: false in application.properties
b) Set server.port: 0 in application.properties
c) Use the @DisableWebServer annotation
d) Use a command-line argument to set the server port to 0
Answer 23
b) Set server.port: 0 in application.properties
Setting the server port to 0 effectively disables the embedded web server.
24. How can you switch from using the default Tomcat server to Jetty in a Spring Boot application?
a) Modify server.xml
b) Change the server property in application.properties
c) Exclude Tomcat and include Jetty dependencies in pom.xml
d) Use @EnableJetty annotation
Answer 24
c) Exclude Tomcat and include Jetty dependencies in pom.xml
Tomcat is included by default, so you must exclude it and add Jetty’s dependencies to switch the server.
25. What is Actuator in Spring Boot?
a) A tool to monitor and manage applications
b) A GUI design tool
c) A security framework
d) A configuration tool
Answer 25
a) A tool to monitor and manage applications
Spring Boot Actuator provides various endpoints for monitoring and managing the application, such as health checks and metrics.
We would love to hear from you! Please leave your comments and share your scores in the section below