0
0
Spring Bootframework~20 mins

@PathVariable for URL parameters in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PathVariable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Spring Boot controller method?
Consider this Spring Boot controller method:
@GetMapping("/users/{id}")
public String getUser(@PathVariable String id) {
    return "User ID: " + id;
}

What will be the response body when a client requests /users/42?
Spring Boot
@GetMapping("/users/{id}")
public String getUser(@PathVariable String id) {
    return "User ID: " + id;
}
AUser ID: 42
BUser ID: {id}
CUser ID: null
D404 Not Found error
Attempts:
2 left
💡 Hint
The @PathVariable annotation binds the URL part to the method parameter.
📝 Syntax
intermediate
2:00remaining
Which option correctly uses @PathVariable with a different parameter name?
Given the URL pattern /items/{itemId}, which method signature correctly binds the path variable to a method parameter named id?
A
@GetMapping("/items/{itemId}")
public String getItem(@PathVariable("id") String id) { return id; }
B
@GetMapping("/items/{itemId}")
public String getItem(@PathVariable String itemId) { return id; }
C
@GetMapping("/items/{id}")
public String getItem(@PathVariable("itemId") String id) { return id; }
D
@GetMapping("/items/{itemId}")
public String getItem(@PathVariable("itemId") String id) { return id; }
Attempts:
2 left
💡 Hint
Use @PathVariable with the exact name of the path segment if the parameter name differs.
🔧 Debug
advanced
2:00remaining
What error occurs with this controller method?
Examine this Spring Boot controller method:
@GetMapping("/orders/{orderId}")
public String getOrder(@PathVariable int id) {
    return "Order: " + id;
}

What happens when a client requests /orders/123?
Spring Boot
@GetMapping("/orders/{orderId}")
public String getOrder(@PathVariable int id) {
    return "Order: " + id;
}
AHTTP 400 Bad Request due to type mismatch
BHTTP 400 Bad Request due to missing path variable binding
CHTTP 404 Not Found error
DReturns 'Order: 123' successfully
Attempts:
2 left
💡 Hint
Check if the method parameter name matches the path variable name or if @PathVariable specifies the name.
state_output
advanced
2:00remaining
What is the output of this controller with multiple @PathVariable parameters?
Given this controller method:
@GetMapping("/books/{genre}/{id}")
public String getBook(@PathVariable String genre, @PathVariable int id) {
    return genre + "-" + id;
}

What is the response when the URL /books/scifi/7 is requested?
Spring Boot
@GetMapping("/books/{genre}/{id}")
public String getBook(@PathVariable String genre, @PathVariable int id) {
    return genre + "-" + id;
}
Abooks-scifi-7
Bscifi-null
Cscifi-7
D7-scifi
Attempts:
2 left
💡 Hint
Each @PathVariable binds to the matching segment in the URL path.
🧠 Conceptual
expert
2:00remaining
Which statement about @PathVariable is true?
Choose the correct statement about the use of @PathVariable in Spring Boot controllers.
A@PathVariable can bind multiple path segments separated by slashes into a single parameter using regex.
B@PathVariable requires the method parameter name to always match the path variable name in the URL.
C@PathVariable cannot be used with optional URL parameters.
D@PathVariable automatically converts path variables to any Java type without errors.
Attempts:
2 left
💡 Hint
Think about advanced usage of @PathVariable with patterns.