Spring Boot REST API MCQ

Representational State Transfer (REST) has taken the software industry by storm, becoming the standard architecture for building web services. With Spring Boot's power and ease of development, building RESTful APIs has never been easier. For beginners stepping into this realm, understanding the fundamentals and quirks of Spring Boot's REST support is essential. Test your foundational knowledge with this set of MCQs!

1. What does REST stand for?

a) Remote System Transfer
b) Representational State Transformation
c) Remote Server Test
d) Representational State Transfer

Answer:

d) Representational State Transfer

Explanation:

REST stands for Representational State Transfer, a standard architecture for building web services.

2. Which annotation is used to create a RESTful controller in Spring Boot?

a) @RestController
b) @Service
c) @Component
d) @Configuration

Answer:

a) @RestController

Explanation:

The @RestController annotation in Spring Boot is used to mark a class as a RESTful web service controller.

3. How can you specify the HTTP method for an endpoint in Spring Boot?

a) @HttpMethod
b) @RequestMethod
c) @HttpRequestMapping
d) @GetMapping, @PostMapping, etc.

Answer:

d) @GetMapping, @PostMapping, etc.

Explanation:

Spring Boot provides annotations like @GetMapping, @PostMapping, @PutMapping, etc., to specify the HTTP method for an endpoint.

4. Which annotation is used to capture a path variable from a URL in Spring Boot?

a) @RequestParam
b) @PathVariable
c) @RequestAttribute
d) @RequestHeader

Answer:

b) @PathVariable

Explanation:

The @PathVariable annotation is used to capture a variable from the URI path.

5. Which of the following is the default embedded servlet container for Spring Boot?

a) Jetty
b) JBoss
c) Tomcat
d) Glassfish

Answer:

c) Tomcat

Explanation:

Tomcat is the default embedded servlet container for Spring Boot.

6. How do you define properties in Spring Boot?

a) @Property
b) .properties or .yml files
c) XML files
d) Java Configurations only

Answer:

b) .properties or .yml files

Explanation:

In Spring Boot, properties can be defined using .properties or .yml files.

7. How can you specify a different context path for your Spring Boot application?

a) server.servlet.context-path
b) spring.application.context
c) server.application.path
d) spring.server.path

Answer:

a) server.servlet.context-path

Explanation:

The server.servlet.context-path property in Spring Boot is used to set a different context path.

8. Which annotation in Spring Boot is used to bind the properties in a properties file to a POJO?

a) @BindProperties
b) @ConfigProperties
c) @ConfigurationProperties
d) @MapProperties

Answer:

c) @ConfigurationProperties

Explanation:

The @ConfigurationProperties annotation binds properties to a POJO.

9. What is the primary role of the @SpringBootApplication annotation in Spring Boot?

a) It starts the embedded Tomcat server.
b) It performs a component scan, enables auto-configuration, and is a Spring Boot configuration.
c) It only configures Spring MVC.
d) It initializes all beans at startup.

Answer:

b) It performs a component scan, enables auto-configuration, and is a Spring Boot configuration.

Explanation:

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

10. How can you externalize configuration in Spring Boot for different environments?

a) Using multiple properties files.
b) Using @Profile annotation.
c) Using XML configuration.
d) Both a and b.

Answer:

d) Both a and b.

Explanation:

Spring Boot allows externalized configuration using multiple properties files (like application-dev.properties, application-prod.properties) and also with the use of @Profile annotation.

11. In Spring Boot, how can you specify that a method parameter should be bound to a web request body?

a) @RequestParam
b) @RequestBody
c) @RequestHeader
d) @ModelAttribute

Answer:

b) @RequestBody

Explanation:

The @RequestBody annotation in Spring Boot is used to indicate that a method parameter should be bound to the body of the web request.

12. What does the @ResponseStatus annotation do?

a) Sets the default status code for a complete class.
b) Maps exceptions to HTTP status codes.
c) Indicates the HTTP status code that should be returned for a specific request method.
d) None of the above.

Answer:

c) Indicates the HTTP status code that should be returned for a specific request method.

Explanation:

The @ResponseStatus annotation can be used on a method or exception class to define the HTTP status that should be returned.

13. Which of the following can be used to enable Cross-Origin Resource Sharing (CORS) globally in a Spring Boot application?

a) @EnableCORS
b) @CrossOrigin at the class level.
c) WebMvcConfigurer with addCorsMappings method.
d) @EnableWebMvcSecurity

Answer:

c) WebMvcConfigurer with addCorsMappings method.

Explanation:

For global CORS configuration, a bean that implements WebMvcConfigurer and overrides addCorsMappings(CorsRegistry registry) can be used.

14. Which annotation is used to handle exceptions in Spring Boot RESTful services?

a) @ExceptionHandler
b) @ControllerAdvice
c) Both a and b.
d) @RestExceptionHandler

Answer:

c) Both a and b.

Explanation:

While @ExceptionHandler is used for handling exceptions within a single controller, @ControllerAdvice can be used to handle exceptions across multiple controllers.

15. Which of the following helps in versioning of a REST API in Spring Boot?

a) Adding version in the URI.
b) Using request parameter versioning.
c) Using custom headers for versioning.
d) All of the above.

Answer:

d) All of the above.

Explanation:

All the mentioned techniques can be used for versioning a REST API in Spring Boot. The choice depends on the specific use-case and requirements.

16. Which of the following is NOT an HTTP method?

a) GET
b) POST
c) SET
d) DELETE

Answer:

c) SET

Explanation:

SET is not a standard HTTP method. The standard methods include GET, POST, PUT, DELETE, etc.

17. Which annotation can be used to inject values from application.properties directly into a field?

a) @Value
b) @Property
c) @InjectValue
d) @ConfigurationValue

Answer:

a) @Value

Explanation:

The @Value annotation is used to inject values from property files into fields in Spring Boot.

18. Which of the following is a blocking paradigm?

a) WebFlux
b) Servlet
c) Reactor
d) RxJava

Answer:

b) Servlet

Explanation:

Servlet is a blocking paradigm, while WebFlux, Reactor, and RxJava are non-blocking and often associated with reactive programming.

19. Which Spring Boot starter dependency is typically added for building RESTful web services?

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

Answer:

a) spring-boot-starter-web

Explanation:

For building RESTful web services, spring-boot-starter-web is typically added as a dependency.

20. Which of the following is used to customize the default behavior of Spring Boot auto-configuration?

a) @Autowired
b) @ComponentScan
c) @Bean
d) @Configuration

Answer:

c) @Bean

Explanation:

While @Configuration is used to declare a class as a source of bean definitions, the @Bean annotation indicates that a method produces a bean to be managed by the Spring container, allowing customization of auto-configured beans.



Comments