Spring Boot Web Applications MCQ

This MCQ guide focuses on key aspects of building web applications using Spring Boot. Ready to challenge yourself? Let's dive in!

1. Which starter dependency is commonly used to develop web applications with Spring Boot?

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

Answer:

b) spring-boot-starter-web

Explanation:

The spring-boot-starter-web dependency includes all the necessary modules to develop web and RESTful applications with Spring Boot.

2. How does Spring Boot automatically configure your web application?

a) By scanning the classpath
b) By reading the application.properties file
c) By analyzing your project structure
d) Through manual configurations

Answer:

a) By scanning the classpath

Explanation:

Spring Boot auto-configures your application based on the libraries you have in your project.

3. Which annotation is used to define a RESTful web service endpoint in a Spring Boot application?

a) @WebEndpoint
b) @Service
c) @RestController
d) @WebController

Answer:

c) @RestController

Explanation:

The @RestController annotation is used to define a RESTful web service endpoint.

4. What does the @RequestMapping annotation do?

a) Maps a URL pattern to a method
b) Injects dependencies
c) Configures properties
d) Starts the application

Answer:

a) Maps a URL pattern to a method

Explanation:

@RequestMapping is used to map web requests onto specific handler classes and handler methods.

5. Which embedded server does Spring Boot use by default for web applications?

a) Jetty
b) WildFly
c) GlassFish
d) Tomcat

Answer:

d) Tomcat

Explanation:

By default, Spring Boot uses Tomcat as the embedded servlet container for web applications.

6. How do you change the default port (8080) of a Spring Boot web application?

a) server.web.port
b) spring.server.port
c) server.port
d) spring.boot.port

Answer:

c) server.port

Explanation:

By setting the server.port property, you can change the default port of a Spring Boot web application.

7. Which annotation is used to inject request parameters into controller methods?

a) @InjectParam
b) @WebParam
c) @RequestParam
d) @Parameter

Answer:

c) @RequestParam

Explanation:

The @RequestParam annotation is used to bind request parameters to a method parameter in your controller.

8. What is the purpose of @PathVariable in Spring Boot?

a) To bind a method parameter to a path variable
b) To define a global variable
c) To handle exceptions
d) To inject dependencies

Answer:

a) To bind a method parameter to a path variable

Explanation:

@PathVariable is used to extract values from the URI path.

9. How can you serve static content like HTML, CSS, and JavaScript in Spring Boot?

a) By placing them in the src/webapp/ directory
b) By placing them in the src/main/static/ directory
c) Using a separate web server
d) Spring Boot doesn't serve static content

Answer:

b) By placing them in the src/main/static/ directory

Explanation:

Static content can be served from the src/main/static/ directory in Spring Boot.

10. Which annotation helps you handle exceptions in Spring Boot web applications?

a) @ExceptionHandler
b) @ErrorController
c) @ExceptionController
d) @ErrorResolver

Answer:

a) @ExceptionHandler

Explanation:

The @ExceptionHandler annotation is used to define a method that will be used to handle exceptions.

11. What does the @SpringBootApplication annotation implicitly include?

a) @SpringBoot
b) @EnableAutoConfiguration, @ComponentScan, and @Configuration
c) @WebSpringBoot
d) @WebConfiguration

Answer:

b) @EnableAutoConfiguration, @ComponentScan, and @Configuration

Explanation:

The @SpringBootApplication annotation is a convenience annotation that adds @Configuration, @EnableAutoConfiguration, and @ComponentScan.

12. To expose a series of endpoints as part of a REST API, what would you typically use in Spring Boot?

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

Answer:

a) @RestController

Explanation:

@RestController is used for defining a RESTful controller which can expose a series of endpoints as part of an API.



Comments