Spring Boot Quiz - MCQ - Multiple Choice Questions

This post contains a few useful Spring Boot (MCQ) multiple-choice questions (quiz) to self-test your knowledge of the Spring Boot framework.

Learn and Master Spring Boot: Master Spring Boot


The answers to each question have been given at the end of this post.

Q1. Spring Boot is used for developing?

  1. Web applications
  2. Distributed applications (Restful web services)
  3. Microservices
  4. All of the above

Answer:

4. All of the above

Explanation:

Spring boot is used to build Web applications, REST APIs, and Microservices.

Q2. What is Spring Initializer?

  1. Web-based tool to bootstrap Spring projects and generate project skeletons with the necessary configurations and dependencies.
  2. A command-line tool for generating a basic Spring Boot project structure
  3. A tool for initializing a Spring-based application with a specific set of dependencies
  4. A tool for creating a Spring Boot application with a specific set of features

Answer:

1. Web-based tool to bootstrap Spring projects and generate project skeletons with the necessary configurations and dependencies.

Explanation:

Spring Initializr is a web-based tool provided by the Spring team to bootstrap Spring projects and generate project skeletons with the necessary configurations and dependencies. It simplifies the process of creating a new Spring-based application by providing an intuitive user interface to customize and generate project structures.

Q3. What does Spring Boot do to simplify the configuration of a Spring-based application?

  1. Automatically configures necessary beans and dependencies
  2. Uses annotations to configure beans and dependencies
  3. Allows for the use of XML configuration files
  4. Provides a command line interface for configuring the application

Answer:

1. Automatically configures necessary beans and dependencies

Explanation:

Spring Boot's auto-configuration feature automatically configures the application based on the classpath dependencies and predefined conventions. It scans the classpath for libraries and frameworks and configures the application accordingly, reducing the need for manual configuration.

Q4. How can a Spring Boot application be packaged and distributed? 

  1. As a JAR file
  2. As a WAR file
  3. As a ZIP file
  4. All of the above

Answer:

4. All of the above

Explanation:

Spring Boot application can be distributed as JAR, WAR, and Zip files.

Q5. Which Spring annotation is used to create RESTful web services using Spring MVC?

  1. @RestController
  2. @Controller
  3. @Component
  4. @Rest

Answer:

1. @RestController

Explanation:

When you annotate a class with @RestController, it indicates that the class is a Spring MVC REST controller responsible for handling incoming HTTP requests and generating the appropriate HTTP responses.

Q6. @RestController annotation is a combination of the below two annotations

  1. @Component and @ResponseBody annotations
  2. @Controller and @ResponseBody annotations
  3. @Service and @ResponseBody annotations
  4. None of the above

Answer:

2. @Controller and @ResponseBody annotations

Explanation:

@RestController combines the functionality of the @Controller and @ResponseBody annotations into a single annotation, making it convenient for creating RESTful APIs.

Q7. Which Spring annotation is used to handle HTTP POST requests?

  1. @GetMapping
  2. @PutMapping
  3. @CreateMapping
  4. @PostMapping

Answer:

4. @PostMapping

Explanation:

The @PostMapping annotation is used to map an HTTP POST request to a specific handler method in a Spring MVC controller. It is a shortcut for specifying the @RequestMapping annotation with the RequestMethod.POST method.

Q8. Which Spring annotation is used to handle HTTP GET requests?

  1. @GetMapping
  2. @PutMapping
  3. @CreateMapping
  4. @PostMapping

Answer:

1. @GetMapping

Explanation:

The @GetMapping annotation is used to map an HTTP GET request to a specific handler method in a Spring MVC controller. It is a shortcut for specifying the @RequestMapping annotation with the RequestMethod.GET method.

Q9. Which Spring annotation is used to handle HTTP DELETE requests?

  1. @GetMapping
  2. @PutMapping
  3. @DeleteMapping
  4. @PostMapping

Answer:

3. @DeleteMapping

Explanation:

The @DeleteMapping annotation is used to map an HTTP DELETE request to a specific handler method in a Spring MVC controller. It is a shortcut for specifying the @RequestMapping annotation with the RequestMethod.DELETE method.

Q10. Which Spring annotation is used to handle HTTP PUT requests?

  1. @GetMapping
  2. @PutMapping
  3. @DeleteMapping
  4. @PostMapping

Answer:

2. @PutMapping

Explanation:

The @PutMapping annotation is used to map an HTTP PUT request to a specific handler method in a Spring MVC controller. It is a shortcut for specifying the @RequestMapping annotation with the RequestMethod.PUT method.

Q11. Which annotation do we use to mark the class as a Service class/component?

  1. @Component
  2. @Service
  3. @Controller
  4. @Repository

Answer:

2. @Service

Explanation:

The @Service annotation is used in Spring Framework to mark a class as a service component. It is typically applied to classes that perform business logic, transaction management, or other services in an application.

Q12. Which is the default implementation class of the JpaRepository interface?

  1. SimpleJpaRepository class
  2. JpaRepositoryImpl class
  3. CustomJpaRepository class
  4. DefaultJpaRepository class

Answer:

1. SimpleJpaRepository class

Explanation:

The SimpleJpaRepository class is a default implementation of the JpaRepository interface in Spring Data JPA. It is provided by the Spring Data JPA framework and serves as a generic repository implementation for performing common CRUD (Create, Read, Update, Delete) operations and other database operations.

Q13. Which is the default HTML template in Spring Boot

  1. JSP
  2. Freemarker
  3. Thymeleaf
  4. Groovy

Answer:

3. Thymeleaf

Explanation:

The default HTML template engine in Spring Boot is Thymeleaf. Thymeleaf is a modern server-side Java template engine that is designed for both web and standalone environments. 

When you create a new Spring Boot project with spring-boot-starter-thymeleaf dependency. Spring boot enables Thymeleaf as the default HTML template engine. Spring Boot automatically configures Thymeleaf with sensible defaults, making it straightforward to start using Thymeleaf for rendering HTML templates in your application.

Q14. Which starter dependency is used to develop web applications or Restful web services?

  1. spring-boot-starter-data-jpa
  2. spring-boot-starter-web
  3. spring-boot-starter-rest
  4. spring-boot-starter-web-dependency

Answer:

2. spring-boot-starter-web

Explanation:

The spring-boot-starter-web dependency provides the necessary components to develop Web Applications and RESTful API in a Spring Boot project, including the embedded Tomcat server, Spring Web MVC, and other required dependencies.

Q15. What is the purpose of the Spring Boot Actuator? 

  1. To provide production-ready features such as monitoring and metrics 
  2. To provide a way to configure beans using annotations 
  3. To provide a way to run Spring Boot applications as a service 
  4. To provide a way to access the underlying database of a Spring Boot application

Answer:

1. To provide production-ready features such as monitoring and metrics

Explanation:

The purpose of the Spring Boot Actuator is to provide production-ready features and monitoring capabilities for Spring Boot applications. It offers a set of powerful endpoints and tools that enable developers and system administrators to monitor, manage, and interact with a Spring Boot application in a production environment.

Q16. How can you specify the port on which a Spring Boot application runs? 

  1. By modifying the application.properties file 
  2. By modifying the application.yml file 
  3. By using the --server.port command-line option 
  4. All of the above

Answer:

4. All of the above

Explanation:

Spring Boot provides several ways to change the server port on which your application listens. You can choose the most convenient option based on your specific requirements and preferences. 

Here are some common methods to change the server port in Spring Boot: 
application.properties or application.yml: You can modify the application.properties or application.yml file in your Spring Boot project and set the server.port property to the desired port number. 

Command-line argument: You can pass a command-line argument --server.port=8081 when starting the Spring Boot application. This will override the port configured in the application.properties or application.yml file. 

System properties: You can set a system property server.port with the desired port number when starting the application. For example, you can use -Dserver.port=8081 as a JVM system property. 

Environment variable: You can set an environment variable SERVER_PORT with the desired port number. Spring Boot will automatically pick up the environment variable and use it as the server port.

Programmatically: If you need more dynamic control over the server port, you can programmatically configure it in your Spring Boot application code. You can use the ServerProperties bean to set the port programmatically.

Q17. What is the purpose of the Spring Boot DevTools? 

  1. To provide a way to run Spring Boot applications in a development environment 
  2. To provide hot-reloading of code changes and automatic application restart
  3. To provide a way to run Spring Boot applications as a service 
  4. To provide a way to access the underlying database of a Spring Boot application

Answer:

2. To provide hot-reloading of code changes and automatic application restart

Explanation:

One of the primary features of DevTools is automatic application restart. When enabled, any changes made to the application's classpath resources (such as Java files, configuration files, templates, or static resources) trigger an automatic restart of the application. This eliminates the need for manual restarts during development, saving time and improving productivity.

DevTools includes live reload functionality that automatically refreshes the browser when changes are detected in client-side resources such as HTML, CSS, or JavaScript files. It enables you to see the changes immediately without manually refreshing the page, making frontend development more efficient.

Q18. What is the difference between Spring and Spring Boot? 

  1. Spring is a Java framework, while Spring Boot is a Java library 
  2. Spring Boot is an opinionated version of Spring, providing a set of default configurations 
  3. Spring Boot is a lightweight version of Spring, while Spring is a full-featured framework 
  4. Spring Boot is a front-end framework, while Spring is a back-end framework 

Answer:

2. Spring Boot is an opinionated version of Spring, providing a set of default configurations

Explanation:

Spring is a comprehensive application development framework, while Spring Boot is a framework built on top of Spring that simplifies and accelerates the development of Spring-based applications. Spring Boot provides an opinionated and streamlined approach with automatic configuration, starters, and embedded servers, while Spring offers more flexibility and control over configuration and application development.

Q19. What is the default Logging implementation offered by Spring Boot?

  1. By using Log4j 
  2. By using Logback 
  3. By using SLF4J 
  4. By using JUL (Java Util Logging)

Answer:

2. By using Logback

Explanation:

By default, Spring Boot prefers Logback for its rich features and simple configuration.

Q20. What is the @SpringBootApplication annotation used for? 

  1. To enable Spring Boot auto-configuration 
  2. To define a Spring Boot starter class 
  3. To define a Spring Boot controller 
  4. To define a Spring Boot service

Answer:

1. To enable Spring Boot auto-configuration

Explanation:

By including @SpringBootApplication, the class benefits from the auto-configuration, component scanning, and other features provided by Spring Boot.

Q21. Which annotation Spring Boot provides for Integration testing?

  1. @SpringBootTest annotation
  2. @WebMvcTest annotation
  3. @DataJpaTest annotation
  4. None of the above

Answer:

1. @SpringBootTest annotation

Explanation:

Spring Boot provides the @SpringBootTest annotation for integration testing. 

The @SpringBootTest annotation is used to specify that a particular test class is an integration test that requires the Spring Boot application context to be created. It loads the complete application context and allows you to test your application in an environment that closely resembles the production setup.

Q22. Which annotation is used to unit test Spring MVC Controllers?

  1. @SpringBootTest annotation
  2. @WebMvcTest annotation
  3. @DataJpaTest annotation
  4. None of the above

Answer:

2. @WebMvcTest annotation

Explanation:

The @WebMvcTest annotation is used to unit test Spring MVC controllers in Spring Boot. 

The @WebMvcTest annotation provides a convenient way to test the controllers in isolation, without loading the entire application context. It focuses on testing the web layer of the application by auto-configuring only the relevant components, such as the controllers, and their dependencies.

Q23. Which Spring Boot annotation is used to unit test the Spring Data JPA repository?

  1. @SpringBootTest annotation
  2. @WebMvcTest annotation
  3. @DataJpaTest annotation
  4. None of the above

Answer:

3. @DataJpaTest annotation

Explanation:

The @DataJpaTest annotation is used to unit test Spring Data JPA repositories in Spring Boot. 

The @DataJpaTest annotation provides a convenient way to test the repository layer of your application by auto-configuring only the necessary components related to Spring Data JPA. It sets up an in-memory database and initializes the EntityManager and necessary Spring Data JPA infrastructure for testing the repositories.

Q24. What are the Spring annotations used for Exception handling?

  1. @ControllerAdvice
  2. @ExceptionHandler
  3. @ResponseStatus
  4. All of the above

Answer:

4. All of the above

Explanation:

Some of the commonly used Spring annotations for exception handling are: 
@ControllerAdvice: The @ControllerAdvice annotation is used to define a global exception handler that applies to all controllers within the application. 

@ExceptionHandler: The @ExceptionHandler annotation is used to define a method that handles a specific type of exception. It is typically used in conjunction with the @ControllerAdvice annotation. 

@ResponseStatus: The @ResponseStatus annotation is used to specify the HTTP response status code that should be sent back to the client when a particular exception occurs.

Q25.  Minimum Java version used for Spring Boot 3?

  1. Java 8
  2. Java 11
  3. Java 17
  4. Java 10

Answer:

3. Java 17

Explanation:

To use Spring Boot 3, you need a minimum of Java 17. It will not work with Java 8 or 11. If you are using Java lower than version 17, then you need to upgrade Java first, and then upgrade/use Spring Boot 3.

Q26. What is the difference between PUT and PATCH in REST API? 

  1. PUT completely update an existing resource, while PATCH modifies only the fields specified in the request body 
  2. PUT is used to update the resource, while PATCH is used to create a resource 
  3. PUT is used to retrieve data, while PATCH is used to update data 
  4. PUT is used to delete the resource, while PATCH is used to update the resource

Answer:

1. PUT completely update an existing resource, while PATCH modifies only the fields specified in the request body

Explanation:

The PUT method is used to completely replace or update an existing resource with a new representation provided in the request payload.

The PATCH method is used to make a partial update to an existing resource. It is intended to modify specific attributes or properties of the resource, rather than replacing the entire resource.

Conclusion

Congratulations on completing the Spring Boot quiz! We hope it challenged your knowledge and provided valuable insights into Spring Boot concepts. Assessing your understanding of the framework is crucial for building robust and efficient applications. 


Keep learning, keep coding, and embrace the power of Spring Boot!

Comments