@PathVariable & @RequestParam — Concept Check
Questions on @PathVariable & @RequestParam. @PathVariable extracts template variables from the URI path; @RequestParam binds query-string parameters, both support type conversion and defaulting.
Questions in this quiz
8 questions on @PathVariable & @RequestParam — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer wants to extract a numeric ID from a URL path using @PathVariable. Which of the following is the correct way to ensure only numeric IDs are matched?
- AUse @PathVariable("id") String id with a regex constraint
- BUse @PathVariable int id with a regex constraint
- CUse @PathVariable Long id with a regex constraint
- DUse @PathVariable Long id without constraints
2.A service endpoint uses @RequestParam to bind a query parameter. If the parameter is not provided in the request, what happens by default?
- AThe parameter is set to a default value
- BThe parameter is set to an empty string
- CThe parameter is set to null
- DThe request fails with an error
3.A developer uses @RequestParam to bind query parameters to a Map<String, String>. What is the primary advantage of this approach?
- AIt captures all query parameters dynamically
- BIt allows binding multiple values to a single key
- CIt requires all parameters to be explicitly listed
- DIt automatically validates parameter values
4.In a Spring application, a developer wants to bind a query parameter to a List<String> using @RequestParam. Which URL format supports this?
- A/api/products?tags=[java,spring]
- B/api/products?tags=java;tags=spring
- C/api/products?tags=java,spring
- D/api/products?tags=java&tags=spring
5.A developer uses @PathVariable to extract a value from a URL path. What happens if the URL segment does not match the expected type?
- AThe request is processed with a null value
- BThe request fails with a type conversion error
- CThe request is processed with a default value
- DThe request is ignored and continues
6.A developer uses @RequestParam with a defaultValue. What happens if the query parameter is missing in the request?
- AThe parameter is set to null
- BThe parameter is set to the default value
- CThe parameter is ignored
- DThe request fails with an error
7.A developer wants to use @PathVariable to bind a path segment with a hyphen. How should it be handled?
- AUse @PathVariable directly with no changes
- BUse @PathVariable with a regex constraint
- CUse @PathVariable with a renamed variable
- DUse @RequestParam instead
8.Which of the following is true about @RequestParam in Spring?
- AIt cannot bind to a Map
- BIt is optional by default
- CIt can only bind to primitive types
- DIt supports automatic type conversion