Spring Boot MCQ Questions and Answers | Set 8

Welcome to Set 8 of our 100+ Spring Boot MCQ Questions and Answers series. This Set 8 continues from MCQs 71-80 and covers Spring Boot and Spring annotations.

Complete Spring Boot MCQ Questions and Answers series:

Spring Boot MCQ Questions and Answers | Set 1

Spring Boot MCQ Questions and Answers | Set 2

Spring Boot MCQ Questions and Answers | Set 3

Spring Boot MCQ Questions and Answers | Set 4

Spring Boot MCQ Questions and Answers | Set 5

Spring Boot MCQ Questions and Answers | Set 6

Spring Boot MCQ Questions and Answers | Set 7

Spring Boot MCQ Questions and Answers | Set 8

Spring Boot MCQ Questions and Answers | Set 9

Spring Boot MCQ Questions and Answers | Set 10

71. In Spring Boot, how can you handle exceptions globally?

a) By using the @ExceptionHandler annotation within a specific controller
b) By defining a global exception handler with the @ControllerAdvice annotation
c) Through XML configuration
d) By creating a custom error.jsp page

Answer:

b) By defining a global exception handler with the @ControllerAdvice annotation

Explanation:

The @ControllerAdvice annotation allows you to handle exceptions across the whole application, not just to an individual controller. It's a type of @Component used to declare exception handlers.

72. How does Spring Boot support form handling in web applications?

a) By using the @FormHandler annotation
b) Through a dedicated form handling module
c) By defining controller methods with annotations like @PostMapping
d) By manual configuration in application.properties

Answer:

c) By defining controller methods with annotations like @PostMapping

Explanation:

Form submissions in Spring Boot are typically handled by controller methods annotated with @PostMapping, which deals with HTTP POST requests commonly associated with forms.

73. How can you define custom error pages in a Spring Boot web application?

a) By adding error templates in the /error directory
b) Through XML configuration
c) By creating a custom ErrorController
d) By using the @ErrorPage annotation

Answer:

a) By adding error templates in the /error directory

Explanation:

In Spring Boot, custom error pages can be defined by adding error templates in the /error directory. This directory can contain static HTML files or templates named after the error status code.

74. What is the purpose of the @RestControllerAdvice annotation in Spring Boot?

a) To provide global exception handling for RESTful controllers
b) To configure RESTful endpoints
c) To handle form submissions
d) To configure application security

Answer:

a) To provide global exception handling for RESTful controllers

Explanation:

The @RestControllerAdvice annotation is a convenience annotation that combines @ControllerAdvice and @ResponseBody. It provides a global exception handling specifically for RESTful controllers.

75. How can you define custom HTTP error codes in a Spring Boot application?

a) By using the @ResponseStatus annotation on exception classes
b) Through XML configuration
c) By configuring them in application.properties
d) By creating a custom ErrorController

Answer:

a) By using the @ResponseStatus annotation on exception classes

Explanation:

Custom HTTP error codes can be defined by using the @ResponseStatus annotation on exception classes. When such an exception is thrown, Spring Boot will use the specified HTTP status code in the response.

76. What is the purpose of the @JsonIgnore annotation in Spring Boot?

a) To ignore fields during JSON serialization and deserialization
b) To configure JSON requests and responses
c) To define database entities
d) To configure application properties

Answer:

a) To ignore fields during JSON serialization and deserialization

Explanation:

The @JsonIgnore annotation is used to mark properties or fields to be ignored during JSON serialization and deserialization, preventing them from being included in the JSON output.

77. In Spring Boot, how can you customize the default JSON serializer?

a) By using the @JsonSerializer annotation
b) Through XML configuration
c) By implementing a custom JsonSerializer
d) By configuring serializer settings in application.properties

Answer:

c) By implementing a custom JsonSerializer

Explanation:

Customizing the default JSON serializer in Spring Boot can be achieved by implementing a custom JsonSerializer. This allows for specific control over how objects are serialized to JSON.

78. How can you serve dynamic HTML content in a Spring Boot application?

a) By using the @HtmlContent annotation
b) Through XML configuration
c) By integrating with template engines like Thymeleaf
d) By using a dedicated HTML processing service

Answer:

c) By integrating with template engines like Thymeleaf

Explanation:

Integrating with template engines like Thymeleaf, FreeMarker, or Mustache can serve dynamic HTML content in Spring Boot. These engines allow the creation of dynamic views.

79. Which Spring Boot starter dependency is used for JPA integrations?

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

Answer:

b) spring-boot-starter-data-jpa

Explanation:

The spring-boot-starter-data-jpa starter is used to integrate JPA into Spring Boot applications. It includes support for Hibernate, Spring Data JPA, and Spring ORM.

80. How does Spring Boot automatically configure a DataSource?

a) By requiring a manual DataSource bean definition
b) Through properties defined in application.properties
c) By using XML-based configuration
d) By scanning the classpath for available DataSource implementations

Answer:

b) Through properties defined in application.properties

Explanation:

Spring Boot auto-configures a DataSource by using properties defined in application.properties or application.yml. It automatically sets up the database connection based on these properties.


Comments