Sample questions
Question 1Easy
Which annotation binds the JSON body of a request to a method parameter?
- @PathVariable
- @RequestParam
- @RequestBody
- @ResponseBody
Answer: C. @RequestBody
Explanation
An HttpMessageConverter (Jackson) deserialises the body into the parameter type.
Step-by-step solution
- A JSON request body is converted to a Java object by an HttpMessageConverter.
- @RequestBody binds that body to the method parameter.
- @PathVariable, @RequestParam and @ResponseBody do other jobs.
- So the answer is @RequestBody.
Why the other options are wrong
- A. @PathVariable reads a value from the URL path.
- B. @RequestParam reads query or form values.
- D. @ResponseBody writes the return value, not the request.
Tip: @RequestBody = JSON request body in. @ResponseBody = return value out.
Question 2Easy
For @GetMapping("/users/{id}"), how is the id extracted?
- @RequestParam Long id
- @RequestBody Long id
- @Header Long id
- @PathVariable Long id
Answer: D. @PathVariable Long id
Explanation
@PathVariable binds URI template variables.
Step-by-step solution
- The URL template /users/{id} contains a variable named id.
- @PathVariable binds that path variable.
- @RequestParam reads query strings, not the path.
- So the answer is @PathVariable Long id.
Why the other options are wrong
- A. @RequestParam reads query parameters, not path variables.
- B. @RequestBody reads the JSON body.
- C. @Header reads request headers.
Tip: {id} in the path = @PathVariable.
Question 3Easy
Which annotation maps HTTP POST requests?
- @CreateMapping
- @PostMapping
- @PutBody
- @GetMapping
Answer: B. @PostMapping
Explanation
@PostMapping is commonly used to create resources.
Step-by-step solution
- POST requests are mapped with a method annotation for POST.
- @PostMapping is the shorthand for that mapping.
- So the answer is option B.
Why the other options are wrong
- A. @CreateMapping is not a Spring annotation.
- C. @PutBody is not a mapping annotation; PUT uses @PutMapping.
- D. @GetMapping maps GET requests, not POST.
Tip: Shorthands exist for each HTTP method: @GetMapping, @PostMapping, @PutMapping, @DeleteMapping.
Question 4Easy
Which annotation maps HTTP GET requests to a handler method?
- @RequestBody
- @GetMapping
- @PathVariable
- @PostMapping
Answer: B. @GetMapping
Explanation
@GetMapping is shorthand for @RequestMapping(method = RequestMethod.GET).
Step-by-step solution
- A handler for HTTP GET is mapped with @GetMapping.
- It is shorthand for @RequestMapping with method GET.
- @PostMapping is for POST, and @PathVariable and @RequestBody read values.
- So the answer is @GetMapping.
Why the other options are wrong
- A. @RequestBody reads the request body.
- C. @PathVariable reads a URL value.
- D. @PostMapping maps POST requests.
Tip: @GetMapping, @PostMapping, @PutMapping and @DeleteMapping map the HTTP verbs.
Question 5Medium
Which class is the modern, fluent synchronous HTTP client introduced in Spring Framework 6.1?
- RestTemplateBuilder only
- HttpURLConnection
- RestClient
- FeignTemplate
Answer: C. RestClient
Explanation
RestClient offers a fluent API for synchronous calls; WebClient is the reactive client.
Step-by-step solution
- Spring Framework 6.1 added RestClient.
- It is a fluent synchronous HTTP client.
- WebClient is the reactive client, and RestTemplateBuilder is an older option.
- So the answer is RestClient.
Why the other options are wrong
- A. RestTemplateBuilder is an older builder, not the new fluent client.
- B. HttpURLConnection is a low-level JDK class.
- D. FeignTemplate is not a Spring class; Feign is a separate library.
Tip: RestClient = modern synchronous client in Spring 6.1+. WebClient = reactive.
Question 6Medium
What status code does a @RestController method return by default when it returns an object normally?
- 204 No Content
- 200 OK
- 201 Created
- 202 Accepted
Answer: B. 200 OK
Explanation
Use @ResponseStatus or ResponseEntity to return other codes.
Step-by-step solution
- A normal return from a @RestController method gives a response.
- Spring uses 200 OK when no other status is set.
- 201, 202 and 204 need an explicit status.
- So the default is 200 OK.
Why the other options are wrong
- A. 204 is used only when there is no body and it is set explicitly.
- C. 201 Created needs an explicit status.
- D. 202 Accepted needs an explicit status.
Tip: Spring defaults to 200 OK; set other codes with ResponseEntity or @ResponseStatus.
Question 7Medium
Which code returns HTTP 201 with the created object as the body?
ResponseEntity.ok().header("Status", "201").body(user)ResponseEntity.accepted().body(user)ResponseEntity.noContent().build()ResponseEntity.status(HttpStatus.CREATED).body(user)
Answer: D. ResponseEntity.status(HttpStatus.CREATED).body(user)
Explanation
ResponseEntity.ok gives 200; noContent gives 204.
Step-by-step solution
- 201 Created is set with HttpStatus.CREATED.
- ResponseEntity.status(...).body(user) sets the status and the body.
- Ok gives 200, accepted gives 202 and noContent gives 204.
- So the answer is ResponseEntity.status(HttpStatus.CREATED).body(user).
Why the other options are wrong
- A. Ok gives 200, and a custom Status header does not set the HTTP code.
- B. Accepted gives 202, not 201.
- C. NoContent gives 204 with no body.
Tip: Use ResponseEntity.status(HttpStatus.CREATED) for 201 with a body.
Question 8Medium
To return 204 No Content from a DELETE endpoint, you can annotate the method with:
- @ResponseBody(HttpStatus.NO_CONTENT)
- @DeleteMapping(status = 204)
- @ResponseStatus(HttpStatus.NO_CONTENT)
- @NoContent on the class
Answer: C. @ResponseStatus(HttpStatus.NO_CONTENT)
Explanation
Alternatively return ResponseEntity.noContent().build().
Step-by-step solution
- @ResponseStatus sets the HTTP status for a handler method.
- Using @ResponseStatus(HttpStatus.NO_CONTENT) gives a 204 response.
- The other options are not valid annotations or attributes.
- So the answer is @ResponseStatus(HttpStatus.NO_CONTENT).
Why the other options are wrong
- A. @ResponseBody does not take a status argument.
- B. @DeleteMapping has no status attribute.
- D. @NoContent is not a Spring annotation.
Tip: @ResponseStatus sets a fixed status; ResponseEntity sets it per response.
Question 9Hard
A handler has @RequestParam String q (no other attributes). A client calls the endpoint without q. What happens?
- q is an empty string
- 400 Bad Request
- 500 Internal Server Error
- q is null
Answer: B. 400 Bad Request
Explanation
@RequestParam is required by default; use required = false, a defaultValue or Optional to make it optional.
Step-by-step solution
- @RequestParam is required by default.
- The client did not send q.
- Spring rejects the request as a bad request.
- So the response is 400 Bad Request.
Why the other options are wrong
- A. A missing parameter is not turned into an empty string.
- C. This is a client error, not a server error.
- D. The request is rejected, so the handler never receives a null q.
Tip: Use required = false, a defaultValue, or Optional for optional request parameters.
Question 10Hard
A controller method returns Optional.empty() from a repository lookup and the API should respond 404. A clean approach is:
- return ResponseEntity.ok(repo.findById(id).get());
- return repo.findById(id).orElseThrow(RuntimeException::new);
- return repo.findById(id).map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build());
- return ResponseEntity.ok(repo.findById(id).orElse(null));
Answer: C. return repo.findById(id).map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build());
Explanation
Mapping the Optional to a ResponseEntity gives 200 with a body or 404; get() would throw on empty.
Step-by-step solution
- FindById returns an Optional, which may be empty.
- map(ResponseEntity::ok) turns a found value into a 200 response.
- orElse(ResponseEntity.notFound().build()) gives 404 when the value is missing.
- So the answer is the map and orElse version.
Why the other options are wrong
- A. get() throws NoSuchElementException on an empty Optional, not a 404.
- B. OrElseThrow gives a server error, not a clean 404 response.
- D. orElse(null) gives 200 with an empty body, not 404.
Tip: Map Optional to ResponseEntity, with orElse for the 404 case.
Practise all 26 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →