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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
Explanation:
The @PostMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.POST).
11. What does the @ResponseBody annotation do?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
Explanation:
spring-boot-starter-web is used for building web, including RESTful, applications using Spring MVC.
20. In which layer is Spring MVC used?
Answer:
Explanation:
Spring MVC is used in the presentation layer to handle web requests and construct responses.