@RestController & @Controller — Concept Check
Questions on @RestController & @Controller. @RestController combines @Controller and @ResponseBody, turning every handler method into a JSON/XML endpoint without needing explicit serialisation annotations.
Questions in this quiz
8 questions on @RestController & @Controller — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.In a Spring Boot application, you have a class annotated with @RestController. What is the default behavior when a method returns an object?
- AThe object is serialized to XML and sent as the response body.
- BThe object is converted to a view name and resolved by a ViewResolver.
- CThe object is ignored, and a 404 status is returned.
- DThe object is serialized to JSON and sent as the response body.
2.A developer uses @Controller for a class and @ResponseBody for a method. What is the result?
- AThe method's return value is resolved by a ViewResolver.
- BThe method returns XML directly to the HTTP response body.
- CThe method's return value is ignored, and a 404 status is returned.
- DThe method returns JSON directly to the HTTP response body.
3.You have a @RestController method returning null. What is the HTTP status code?
- A204 with no content
- B404 not found
- C500 internal server error
- D200 with an empty body
4.A @RestController method returns a ResponseEntity with a 404 status. What happens?
- AThe client receives a 200 status with an empty body.
- BThe client receives a 404 status with no body.
- CThe client receives a 404 status with a default error message.
- DThe client receives a 204 status with no content.
5.In a @Controller, what happens if a method returns a String without @ResponseBody?
- AThe String is ignored, and a 404 status is returned.
- BThe String is serialized to JSON and sent as the response body.
- CThe String is treated as a view name and resolved by a ViewResolver.
- DThe String is serialized to XML and sent as the response body.
6.What is the role of @RestControllerAdvice in a Spring application?
- ASerializes objects to XML by default.
- BResolves view names for MVC controllers.
- CCentralizes exception handling with JSON responses.
- DHandles HTTP requests for static content.
7.A developer mistakenly believes @RestController methods must always return JSON. What is a correct statement?
- AThey must always return JSON.
- BThey can return XML if configured appropriately.
- CThey can only return HTML.
- DThey can only return plain text.
8.A @RestController method returns a list of objects. What is the default serialization format?
- AXML document
- BPlain text list
- CCSV format
- DJSON array