Spring Boot MCQ

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?

a) Provides an embedded database for all applications
b) Forces developers to adhere to a specific programming model
c) Simplifies the process of building production-ready Spring applications
d) Reduces the need for any external dependencies or frameworks

Answer:

c) Simplifies the process of building production-ready Spring applications

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?

a) It initializes Spring Security
b) It’s equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan
c) It sets up Spring MVC
d) It acts as an entry point for the Spring Boot application

Answer:

b) It’s equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan

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?

a) Set spring.port in application.properties
b) Set server.port in application.properties
c) Use @Port annotation
d) Override SpringBootApplication class

Answer:

b) Set server.port in application.properties

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?

a) A starting point for versioned dependencies
b) A web server
c) A database connection
d) An IDE for Spring Boot

Answer:

a) A starting point for versioned dependencies

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?

a) Direct JDBC
b) Spring Data
c) Hibernate
d) All of the above

Answer:

d) All of the above

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?

a) spring-config.xml
b) application.properties
c) config.java
d) settings.yml

Answer:

b) application.properties

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?

a) Spring Boot CLI
b) Spring Initializr
c) Maven Central
d) GitHub Spring Boot Repository

Answer:

b) Spring Initializr

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?

a) Tomcat
b) Jetty
c) Undertow
d) All of the above

Answer:

d) All of the above

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?

a) application-{profile}.properties
b) spring-{profile}.xml
c) profile-config.java
d) settings-{profile}.yml

Answer:

a) application-{profile}.properties

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?

a) spring-boot-starter-web
b) spring-boot-starter-jdbc
c) spring-boot-starter-data
d) spring-boot-starter-app

Answer:

a) spring-boot-starter-web

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?

a) @ConfigurationProperties
b) @Value
c) @PropertySource
d) @InjectProperties

Answer:

a) @ConfigurationProperties

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?

a) log4j.xml
b) logback.xml
c) logging.properties
d) application-logger.properties

Answer:

b) logback.xml

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?

a) To start the Spring MVC dispatcher servlet
b) To automatically configure your application based on the dependencies you have in your project
c) To enable manual configurations
d) To enable WebSocket in the application

Answer:

b) To automatically configure your application based on the dependencies you have in your project

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?

a) banner.txt
b) banner.html
c) banner.json
d) a) and b)

Answer:

d) a) and b)

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?

a) spring-boot-starter-standalone
b) spring-boot-starter-app
c) spring-boot-starter-parent
d) spring-boot-starter-web

Answer:

d) spring-boot-starter-web

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?

a) It provides default configurations for a Spring Boot application
b) It contains the main method to run the application
c) It provides basic authentication features
d) It sets up a database connection

Answer:

a) It provides default configurations for a Spring Boot application

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?

a) By manually importing all classes
b) Using the spring-boot-devtools dependency in the pom.xml
c) By adding @EnableDevTools annotation
d) Using the devtools command in the Spring Boot CLI

Answer:

b) Using the spring-boot-devtools dependency in the pom.xml

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?

a) Using –spring.profiles.active command line argument
b) Using spring.profiles.active in application.properties
c) Both a) and b)
d) By setting the environment variable SPRING_PROFILES_ACTIVE

Answer:

c) Both a) and b)

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?

a) spring-boot-starter-data-jdbc
b) spring-boot-starter-jdbc
c) spring-boot-starter-database
d) spring-boot-starter-hibernate

Answer:

b) spring-boot-starter-jdbc

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?

a) @RestController
b) @Service
c) @Controller
d) @Repository

Answer:

a) @RestController

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?

a) spring-boot-starter-web
b) spring-boot-starter-tomcat
c) Both a) and b)
d) spring-boot-starter-server

Answer:

c) Both a) and b)

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?

a) By using the exclude attribute of @SpringBootApplication annotation
b) By removing the corresponding starter dependency
c) By adding a property in application.properties
d) By annotating the main application class with @ExcludeConfiguration

Answer:

a) By using the exclude attribute of @SpringBootApplication annotation

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?

a) Dependency management
b) Automatic application restarts
c) The ability to create a standalone executable JAR or WAR
d) Integration with Spring Data

Answer:

c) The ability to create a standalone executable JAR or WAR

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?

a) -Dspring.profiles.active=profileName
b) -Dprofile=profileName
c) -Profile spring.profiles.active=profileName
d) –profile=profileName

Answer:

a) -Dspring.profiles.active=profileName

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?

a) @RequestMapping
b) @HttpMethod
c) @RestControllerMethod
d) @WebMapping

Answer:

a) @RequestMapping

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?

a) @Scheduled
b) @IntervalRun
c) @Periodic
d) @FixedRate

Answer:

a) @Scheduled

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?

a) spring-boot-starter-webapi
b) spring-boot-starter-web
c) spring-boot-starter-mvc
d) spring-boot-starter-rest

Answer:

b) spring-boot-starter-web

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?

a) server.port in application.properties
b) application.port in server.properties
c) Using the @Port annotation
d) You can’t change the default port

Answer:

a) server.port in application.properties

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?

a) @JpaRepository
b) @Repository
c) @SpringDataRepo
d) @DataRepository

Answer:

b) @Repository

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?

a) To enable logging
b) To indicate that a component belongs to a specific profile
c) To enable JPA functionalities
d) To run scheduled tasks

Answer:

b) To indicate that a component belongs to a specific profile

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?

a) @RollbackOnError
b) @RevertTransaction
c) @ExceptionRollback
d) @Transactional

Answer:

d) @Transactional

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?

a) Add spring-boot-starter-actuator dependency
b) Annotate the main class with @EnableActuator
c) Include actuator.properties file
d) Add spring.actuator.expose-http=true in application.properties

Answer:

a) Add spring-boot-starter-actuator dependency

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?

a) Using Spring Security
b) By setting management.endpoints.enabled to false
c) By annotating them with @SecureEndpoint
d) Actuator endpoints are secured by default

Answer:

a) Using Spring Security

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.


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top