0
0
Spring Bootframework~20 mins

Handling path variables and query params together in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Path & Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when accessing a Spring Boot endpoint with both path variable and query parameter?
Consider the following Spring Boot controller method:
@GetMapping("/users/{id}")
public String getUser(@PathVariable String id, @RequestParam(required = false) String filter) {
    return "User: " + id + ", Filter: " + (filter == null ? "none" : filter);
}

What will be the response body when a client sends a GET request to /users/42?filter=active?
Spring Boot
@GetMapping("/users/{id}")
public String getUser(@PathVariable String id, @RequestParam(required = false) String filter) {
    return "User: " + id + ", Filter: " + (filter == null ? "none" : filter);
}
AUser: , Filter: active
BUser: filter, Filter: active
CUser: 42, Filter: active
DUser: 42, Filter: none
Attempts:
2 left
💡 Hint
Remember that @PathVariable extracts the part of the URL path, and @RequestParam extracts query parameters.
📝 Syntax
intermediate
2:00remaining
Which method signature correctly handles a path variable and an optional query parameter in Spring Boot?
You want to create a GET endpoint that accepts a path variable 'category' and an optional query parameter 'sort'. Which method signature is correct?
Apublic String listItems(@RequestParam(required = false) String category, @RequestParam String sort)
Bpublic String listItems(@RequestParam String category, @PathVariable String sort)
Cpublic String listItems(@PathVariable String category, @RequestParam String sort)
Dpublic String listItems(@PathVariable String category, @RequestParam(required = false) String sort)
Attempts:
2 left
💡 Hint
Path variables come from the URL path, query parameters come after '?' and can be optional.
🔧 Debug
advanced
2:00remaining
Why does this Spring Boot method fail to bind the path variable correctly?
Given this controller method:
@GetMapping("/products/{id}")
public String getProduct(@RequestParam String id) {
    return "Product " + id;
}

What error or behavior will occur when accessing /products/10?
Spring Boot
@GetMapping("/products/{id}")
public String getProduct(@RequestParam String id) {
    return "Product " + id;
}
AHTTP 400 Bad Request error because 'id' query parameter is missing
BReturns 'Product 10' correctly
CHTTP 404 Not Found error because path variable is not matched
DReturns 'Product null' because id is null
Attempts:
2 left
💡 Hint
Check if the method parameter matches the URL path variable or query parameter.
state_output
advanced
2:00remaining
What is the output of this Spring Boot controller method when called with /orders/5?status=shipped?
Analyze this method:
@GetMapping("/orders/{orderId}")
public String getOrder(@PathVariable int orderId, @RequestParam(defaultValue = "pending") String status) {
    return "Order " + orderId + " is " + status;
}

What will be the response body for the request /orders/5?status=shipped?
Spring Boot
@GetMapping("/orders/{orderId}")
public String getOrder(@PathVariable int orderId, @RequestParam(defaultValue = "pending") String status) {
    return "Order " + orderId + " is " + status;
}
AOrder shipped is 5
BOrder 5 is shipped
COrder 5 is pending
DOrder null is shipped
Attempts:
2 left
💡 Hint
The query parameter 'status' overrides the default value.
🧠 Conceptual
expert
2:00remaining
Which statement best describes handling path variables and query parameters together in Spring Boot?
Select the most accurate statement about using @PathVariable and @RequestParam together in a Spring Boot controller method.
A@PathVariable extracts values from the URL path, while @RequestParam extracts values from the query string; both can be used together in the same method.
B@RequestParam is used only for POST requests, and @PathVariable only for GET requests.
CYou cannot use @PathVariable and @RequestParam in the same method because they conflict.
D@PathVariable extracts query parameters, and @RequestParam extracts path variables.
Attempts:
2 left
💡 Hint
Think about where each annotation gets its data from in the URL.