Bird
Raised Fist0
Spring Bootframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. In Spring Boot, which annotation is used to extract a value from the URL path segment in a controller method?
easy
A. @PathVariable
B. @RequestParam
C. @RequestBody
D. @RequestHeader

Solution

  1. Step 1: Understand URL parts

    The URL path segment is the part of the URL that defines the resource location, like /users/{id}.
  2. Step 2: Identify the correct annotation

    @PathVariable is used to bind a method parameter to a URI template variable in the path.
  3. Final Answer:

    @PathVariable -> Option A
  4. Quick Check:

    Path segment value = @PathVariable [OK]
Hint: Use @PathVariable for URL path parts, not query strings [OK]
Common Mistakes:
  • Confusing @RequestParam with @PathVariable
  • Using @RequestBody for path variables
  • Trying to get path variables from headers
2. Which of the following method signatures correctly handles a path variable id and a query parameter sort in Spring Boot?
easy
A. public String getUser(@PathVariable String id, @RequestParam String sort)
B. public String getUser(@RequestParam String id, @PathVariable String sort)
C. public String getUser(@RequestParam String id, @RequestParam String sort)
D. public String getUser(@PathVariable String id, @PathVariable String sort)

Solution

  1. Step 1: Match annotations to parameter types

    Path variables come from the URL path, so use @PathVariable for id. Query parameters come after ?, so use @RequestParam for sort.
  2. Step 2: Check method signature correctness

    public String getUser(@PathVariable String id, @RequestParam String sort) correctly uses @PathVariable for id and @RequestParam for sort.
  3. Final Answer:

    public String getUser(@PathVariable String id, @RequestParam String sort) -> Option A
  4. Quick Check:

    Path variable = @PathVariable, Query param = @RequestParam [OK]
Hint: Path variables use @PathVariable, query params use @RequestParam [OK]
Common Mistakes:
  • Swapping @PathVariable and @RequestParam
  • Using @PathVariable for query parameters
  • Missing one of the annotations
3. Given the controller method:
@GetMapping("/items/{itemId}")
public String getItem(@PathVariable String itemId, @RequestParam(defaultValue = "asc") String order) {
    return itemId + "-" + order;
}

What will be the output when a client requests /items/42?order=desc?
medium
A. "42-asc"
B. "42-desc"
C. "desc-42"
D. Runtime error due to missing parameter

Solution

  1. Step 1: Identify values from URL

    Path variable itemId is "42" from /items/42. Query parameter order is "desc" from ?order=desc.
  2. Step 2: Understand method return

    The method returns itemId + "-" + order, so "42-desc".
  3. Final Answer:

    "42-desc" -> Option B
  4. Quick Check:

    Path variable + query param = "42-desc" [OK]
Hint: Path variable fills {itemId}, query param fills order value [OK]
Common Mistakes:
  • Ignoring query parameter and using default
  • Mixing order of concatenation
  • Expecting error when query param is present
4. Consider this controller method:
@GetMapping("/books/{bookId}")
public String getBook(@PathVariable int bookId, @RequestParam int page) {
    return "Book: " + bookId + ", Page: " + page;
}

What error will occur if a client calls /books/abc?page=2?
medium
A. 404 Not Found because path variable missing
B. 500 Internal Server Error due to null page
C. 400 Bad Request due to type mismatch
D. No error, returns "Book: abc, Page: 2"

Solution

  1. Step 1: Check path variable type

    The method expects bookId as int, but URL provides "abc" which is not a number.
  2. Step 2: Understand Spring Boot behavior on type mismatch

    Spring Boot returns 400 Bad Request when it cannot convert path variable to required type.
  3. Final Answer:

    400 Bad Request due to type mismatch -> Option C
  4. Quick Check:

    Type mismatch in @PathVariable causes 400 error [OK]
Hint: Non-numeric path variable for int causes 400 error [OK]
Common Mistakes:
  • Expecting 404 Not Found instead of 400
  • Assuming null page causes error
  • Thinking method returns string with invalid input
5. You want to create a Spring Boot endpoint that handles a user ID as a path variable and optionally accepts a query parameter filter. If filter is missing, it should default to "all". Which method signature correctly implements this behavior?
hard
A. public String getUser(@PathVariable String userId, @RequestParam Optional<String> filter)
B. public String getUser(@RequestParam String userId, @RequestParam String filter)
C. public String getUser(@PathVariable String userId, @RequestParam(required = true) String filter)
D. public String getUser(@PathVariable String userId, @RequestParam(defaultValue = "all") String filter)

Solution

  1. Step 1: Identify path variable usage

    User ID is part of the URL path, so use @PathVariable for userId.
  2. Step 2: Handle optional query parameter with default

    Use @RequestParam with defaultValue = "all" to provide a default when filter is missing.
  3. Final Answer:

    public String getUser(@PathVariable String userId, @RequestParam(defaultValue = "all") String filter) -> Option D
  4. Quick Check:

    Default query param value = @RequestParam(defaultValue) [OK]
Hint: Use @RequestParam(defaultValue) for optional query params [OK]
Common Mistakes:
  • Using @RequestParam(required = true) for optional params
  • Using @RequestParam without defaultValue causing errors
  • Confusing @RequestParam and @PathVariable roles
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard