Spring MVC MCQ

Spring MVC, part of the larger Spring framework, is a powerful module that allows for the development of web applications. If you’re a beginner with Spring MVC or need a refresher, this quiz is a great way to test your knowledge. Dive in!

1. What does MVC stand for in Spring MVC?

a) Multi-View Controller
b) Module-View-Controller
c) Model-View-Controller
d) Model-Value-Configuration

Answer:

c) Model-View-Controller

Explanation:

MVC stands for Model-View-Controller, which is a design pattern to separate an application’s concerns.

2. Which annotation is used to create a Spring MVC controller class?

a) @ControllerBean
b) @SpringController
c) @WebController
d) @Controller

Answer:

d) @Controller

Explanation:

The @Controller annotation indicates that a class is a Spring MVC controller.

3. Which of the following handles the HTTP request in Spring MVC?

a) DispatcherServlet
b) HandlerInterceptor
c) HttpListener
d) RequestHandler

Answer:

a) DispatcherServlet

Explanation:

In Spring MVC, the DispatcherServlet is responsible for dispatching incoming HTTP requests to the appropriate controller methods.

4. Which annotation binds a method parameter to a named attribute?

a) @ModelAttribute
b) @ParameterAttribute
c) @BindAttribute
d) @RequestValue

Answer:

a) @ModelAttribute

Explanation:

The @ModelAttribute annotation is used to bind a method parameter to a named attribute, potentially initializing the attribute from a database or other source.

5. How can you handle exceptions in Spring MVC?

a) @ExceptionHandler
b) @CatchException
c) @ErrorResolver
d) @ResolveError

Answer:

a) @ExceptionHandler

Explanation:

The @ExceptionHandler annotation is used to handle exceptions in Spring MVC.

6. Which annotation is used to bind a method parameter to a web request header?

a) @RequestHeader
b) @Header
c) @BindHeader
d) @HTTPHeader

Answer:

a) @RequestHeader

Explanation:

The @RequestHeader annotation is used to bind a method parameter to a specific web request header.

7. In Spring MVC, what is the role of the ViewResolver?

a) Resolving bean dependencies
b) Handling exceptions
c) Resolving views to specific URLs
d) Handling request parameters

Answer:

c) Resolving views to specific URLs

Explanation:

ViewResolver in Spring MVC helps in mapping view names to actual views (like JSPs).

8. Which annotation is used to denote a regular expression in URI template in Spring MVC?

a) @Regex
b) @PathVariable
c) @URIExpression
d) @MatchPattern

Answer:

b) @PathVariable

Explanation:

The @PathVariable annotation can be used to extract values from the URI, and it supports regular expressions.

9. Which Spring MVC module provides integration with RESTful services?

a) Spring RestController
b) Spring REST
c) Spring Web MVC
d) Spring WebFlux

Answer:

c) Spring Web MVC

Explanation:

While @RestController is an annotation used to create RESTful controllers, it’s the Spring Web MVC module that provides integration with RESTful services.

10. Which annotation indicates a method should handle HTTP POST requests?

a) @HttpPost
b) @PostHandler
c) @RequestMapping(method = RequestMethod.POST)
d) @PostMapping

Answer:

d) @PostMapping

Explanation:

The @PostMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.POST).

11. What does the @ResponseBody annotation do?

a) It sends the return value of a method back to the web response body.
b) It binds the parameters of a method to the request body.
c) It triggers an exception handling method.
d) It binds the result of a method to a view.

Answer:

a) It sends the return value of a method back to the web response body.

Explanation:

The @ResponseBody annotation tells a controller that the return value of a method should be written directly to the HTTP response body.

12. How do you redirect to another URL in Spring MVC?

a) “redirect:/url_path”
b) “forward:/url_path”
c) “goto:/url_path”
d) “move:/url_path”

Answer:

a) “redirect:/url_path”

Explanation:

In Spring MVC, to perform a redirect, you return a string starting with “redirect:” followed by the path.

13. Which of the following annotations is used to handle multipart file uploads?

a) @FileUpload
b) @MultipartFile
c) @RequestFile
d) @UploadPart

Answer:

b) @MultipartFile

Explanation:

The @MultipartFile annotation is used to handle multipart file uploads in Spring MVC.

14. How do you specify that a controller method should produce JSON as a response?

a) @Produces(“application/json”)
b) @ResponseBody(type=”json”)
c) @ResponseFormat(“JSON”)
d) @RequestMapping(produces=”application/json”)

Answer:

d) @RequestMapping(produces=”application/json”)

Explanation:

The @RequestMapping annotation with the produces attribute set to “application/json” specifies that the controller method should produce JSON as a response.

15. Which of the following represents a form-backing bean in Spring MVC?

a) @FormEntity
b) @ModelEntity
c) @ModelAttribute
d) @BeanForm

Answer:

c) @ModelAttribute

Explanation:

@ModelAttribute can be used to represent a form-backing bean in Spring MVC.

16. Which of the following components decides which controller method is to be called for a request?

a) DispatcherServlet
b) Controller
c) HandlerMapping
d) ViewResolver

Answer:

c) HandlerMapping

Explanation:

The HandlerMapping component decides which controller method should be called based on the incoming request.

17. What does the @Valid annotation in Spring MVC do?

a) It ensures that the method is correctly overridden from a superclass.
b) It triggers validation of a method parameter or field.
c) It ensures that an HTTP request is valid.
d) It validates the return type of a method.

Answer:

b) It triggers validation of a method parameter or field.

Explanation:

The @Valid annotation triggers validation of the annotated method parameter or field.

18. Which Spring annotation is used to handle Cross-Origin Resource Sharing (CORS) in Spring MVC?

a) @CrossOrigin
b) @CORSHandler
c) @EnableCORS
d) @CORSConfig

Answer:

a) @CrossOrigin

Explanation:

The @CrossOrigin annotation is used in Spring MVC to handle Cross-Origin Resource Sharing (CORS).

19. 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.

20. In which layer is Spring MVC used?

a) Data layer
b) Business layer
c) Presentation layer
d) Integration layer

Answer:

c) Presentation layer

Explanation:

Spring MVC is used in the presentation layer to handle web requests and construct responses.


Leave a Comment

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

Scroll to Top