Last Updated On
Welcome to the SpringBoot Quiz! This blog post features 25 multiple-choice questions that explore the concepts of SpringBoot.
1. What is the primary feature of Spring Boot?
a) Simplifies data access
b) Automates code generation
c) Simplifies project setup
d) Enhances UI design
Answer 1
c) Simplifies project setup
Spring Boot simplifies the development of new Spring applications through convention over configuration and automatic setup.
2. Which annotation is used to mark a Spring Boot application entry point?
a) @Configuration
b) @SpringBootApplication
c) @Component
d) @RestController
Answer 2
b) @SpringBootApplication
The @SpringBootApplication annotation is used to mark the main class of a Spring Boot application.
3. The @SpringBootApplication annotation is used to mark the main class of a Spring Boot application.
a) @Component
b) @EnableConfiguration
c) @Configuration
d) @EnableAutoConfiguration
Answer 3
d) @EnableAutoConfiguration
4. What does @SpringBootApplication annotation include?
a) @ComponentScan
b) @EnableAutoConfiguration
c) @SpringBootConfiguration
d) All of the above
Answer 4
d) All of the above
5. Which starter is used for Spring Boot Rest API applications?
a) spring-boot-starter-jdbc
b) spring-boot-starter-web
c) spring-boot-starter-data-jpa
d) spring-boot-starter-security
Answer 5
b) spring-boot-starter-web
The `spring-boot-starter-web` starter includes the necessary dependencies for developing web applications using Spring MVC. It provides essential features for building RESTful APIs, such as HTTP request handling, JSON serialization and deserialization, and web server capabilities.
6. Which starter is used for Spring Boot web applications?
a) spring-boot-starter-jdbc
b) spring-boot-starter-web
c) spring-boot-starter-data-jpa
d) spring-boot-starter-security
Answer 6
b) spring-boot-starter-web
This starter provides the necessary dependencies to develop web applications using Spring MVC.
7. What is auto-configuration in Spring Boot?
a) Automatic setup of the JVM
b) Automatic import of all Java libraries c) Automatic configuration of beans based on classpath settings
d) Automatic update of Spring Boot versions
Answer 7
c) Automatic configuration of beans based on classpath settings
Auto-configuration refers to Spring Boot’s ability to automatically set up application context based on dependencies available in the classpath.
8. What does the following code snippet achieve?
@Bean
public MyService myService() {
return new MyServiceImpl();
}
a) It creates a prototype bean.
b) It registers a singleton bean.
c) It enables caching.
d) It defines a configuration class.
Answer 8
b) It registers a singleton bean.
The @Bean annotation registers a singleton bean in the Spring application context.
9. Which configuration file is commonly used to define properties in a Spring Boot application?
a) application.yml
b) config.properties
c) spring-config.xml
d) web.xml
Answer 9
a) application.yml
The application.yml file is commonly used for defining configuration properties in a Spring Boot application.
10. What is the purpose of the `spring-boot-starter-test` dependency?
a) To start the application faster
b) To provide a built-in testing framework for JUnit, Mockito, etc.
c) To run performance benchmarks
d) To manage database migrations
Answer 10
b) To provide a built-in testing framework for JUnit, Mockito, etc.
This starter provides testing libraries and frameworks for unit tests and mocking, making testing seamless in Spring Boot applications.
11. Which of the following is the correct ?
@RestController
public class ArticleController {
@GetMapping("/articles")
public List<Article> getAllArticles() {
return articleService.findAll();
}
}
Choose one option
a) The method returns a JSON representation of all articles.
b) The method does not return anything.
c) The endpoint is configured for POST requests.
d) The controller does not require a service.
Answer 11
a) The method returns a JSON representation of all articles.
The @GetMapping annotation defines a GET endpoint that returns a JSON representation of all articles.
12. What does the @Value annotation do in the following code?
@Value("${my.api.key}")
private String apiKey;
Choose one option
a) It injects a bean
b) It maps an environment variable
c) It retrieves a property value from application.properties
d) It initializes a method
Answer 12
c) It retrieves a property value from application.properties
The @Value annotation is used to inject a property value from the application.properties file into a field.
13. What will happen if you try to access a bean that is defined with a prototype scope?
@Bean
@Scope("prototype")
public Article article() {
return new Article();
}
Choose one option
a) The same instance is returned every time
b) A new instance is created for each request
c) An exception is thrown
d) The application will not start
Answer 13
b) A new instance is created for each request
A prototype-scoped bean creates a new instance every time it is requested from the application context
14. Which embedded server does Spring Boot use by default for web applications?
Choose one option
a) JBoss
b) WebLogic
c) Tomcat
d) Jetty
Answer 14
c) Tomcat
Spring Boot initially sets Tomcat as its default embedded server, which you can switch to other options like Jetty or Undertow if needed.
15. Which annotation is used to handle exceptions globally in a Spring Boot application?
Choose one option
a) @Controller
b) @RestControllerAdvice
c) @ExceptionHandler
d) @Service
Answer 15
b) @RestControllerAdvice
The @RestControllerAdvice annotation is used to define global exception handling for REST controllers.
16. Given the following configuration, what is the purpose of the @Profile annotation?
@Profile("dev")
@Bean
public DataSource dataSource() {
return new H2DataSource();
}
Choose one option
a) It marks a bean as singleton
b) It specifies that the bean should only be loaded in the ‘dev’ profile
c) It creates a prototype bean
d) It initializes the application context
Answer 16
b) It specifies that the bean should only be loaded in the ‘dev’ profile
The @Profile annotation indicates that the dataSource bean should only be instantiated when the ‘dev’ profile is active.
17. What does the ‘@RestController’ annotation do?
Choose one option
a) Enables automatic logging Configurator
b) Combines @Controller and @ResponseBody annotations
c) Configures a package
d) Sets the application port
Answer 17
b) Combines @Controller and @ResponseBody annotations
The @RestController annotation is used in Spring MVC applications to handle RESTful web services, simplifying the controller implementation.
18. What dependency is required for embedding an H2 database in a Spring Boot application?
a) spring-boot-starter-data-mongo
b) spring-boot-starter-jdbc
c) spring-boot-starter-data-jpa
d) h2
Answer 18
d) h2
The H2 database is an in-memory database, and its dependency needs to be included in the pom.xml or build.gradle file to be used.
19. What is the function of ‘spring-boot-starter-parent’ in a Maven-based Spring Boot project?
a) To hold all project dependencies
b) To provide default project structure
c) To provide default configuration settings and inherit dependency management
d) To define the project version
Answer 19
c) To provide default configuration settings and inherit dependency management
spring-boot-starter-parent provides default configurations and helps manage dependencies, versions, and default plugins for the project.
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? 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