Bird
Raised Fist0
Spring Bootframework~20 mins

@RequestParam for query strings in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
RequestParam Mastery
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 the endpoint with query ?name=John
Consider this Spring Boot controller method:
@GetMapping("/greet")
public String greet(@RequestParam String name) {
    return "Hello, " + name + "!";
}

What will be the response body when you visit /greet?name=John?
Spring Boot
@GetMapping("/greet")
public String greet(@RequestParam String name) {
    return "Hello, " + name + "!";
}
A"Hello, John!"
B"Hello, null!"
CHTTP 400 Bad Request error
D"Hello, !"
Attempts:
2 left
💡 Hint
The @RequestParam annotation binds the query string parameter to the method argument.
❓ state_output
intermediate
2:00remaining
What happens if the query parameter is missing?
Given this method:
@GetMapping("/welcome")
public String welcome(@RequestParam String user) {
    return "Welcome, " + user;
}

What happens when you call /welcome without any query parameters?
Spring Boot
@GetMapping("/welcome")
public String welcome(@RequestParam String user) {
    return "Welcome, " + user;
}
AHTTP 500 Internal Server Error
B"Welcome, null"
CHTTP 400 Bad Request error
D"Welcome, "
Attempts:
2 left
💡 Hint
By default, @RequestParam is required unless specified otherwise.
📝 Syntax
advanced
2:00remaining
Which option correctly sets a default value for a missing query parameter?
You want to make the query parameter optional and provide a default value "Guest" if missing. Which method signature is correct?
A
@GetMapping("/hello")
public String hello(@RequestParam(required = false, default = "Guest") String name) {
    return "Hi, " + name;
}
B
@GetMapping("/hello")
public String hello(@RequestParam(defaultValue = "Guest") String name) {
    return "Hi, " + name;
}
C
@GetMapping("/hello")
public String hello(@RequestParam(required = true, defaultValue = "Guest") String name) {
    return "Hi, " + name;
}
D
@GetMapping("/hello")
public String hello(@RequestParam(optional = true, defaultValue = "Guest") String name) {
    return "Hi, " + name;
}
Attempts:
2 left
💡 Hint
The correct attribute name for default value is 'defaultValue'.
🔧 Debug
advanced
2:00remaining
Why does this code cause a 400 error when calling /search without parameters?
Look at this method:
@GetMapping("/search")
public String search(@RequestParam String query, @RequestParam(required = false) Integer page) {
    return "Searching for " + query + " on page " + (page == null ? 1 : page);
}

Why does calling /search without query parameters cause a 400 error?
Spring Boot
@GetMapping("/search")
public String search(@RequestParam String query, @RequestParam(required = false) Integer page) {
    return "Searching for " + query + " on page " + (page == null ? 1 : page);
}
ABecause 'query' is required and missing in the request
BBecause the method returns a String instead of ResponseEntity
CBecause both parameters are required by default
DBecause 'page' is an Integer and cannot be null
Attempts:
2 left
💡 Hint
Check which parameters are required by default.
🧠 Conceptual
expert
3:00remaining
How does Spring handle multiple values for the same query parameter?
Given this method:
@GetMapping("/filter")
public String filter(@RequestParam List tags) {
    return "Tags: " + String.join(",", tags);
}

What is the output when calling /filter?tags=java&tags=spring&tags=boot?
Spring Boot
@GetMapping("/filter")
public String filter(@RequestParam List<String> tags) {
    return "Tags: " + String.join(",", tags);
}
A"Tags: java"
B"Tags: [java, spring, boot]"
CHTTP 400 Bad Request error
D"Tags: java,spring,boot"
Attempts:
2 left
💡 Hint
Spring can bind multiple query parameters with the same name into a List.

Practice

(1/5)
1. What does the @RequestParam annotation do in a Spring Boot controller?
easy
A. It maps the URL path to a controller method.
B. It extracts query string parameters from the URL and passes them to the method.
C. It defines the HTTP method type like GET or POST.
D. It handles exceptions thrown by the controller.

Solution

  1. Step 1: Understand the role of @RequestParam

    @RequestParam is used to get values from the URL query string, like ?name=John.
  2. Step 2: Compare with other annotations

    @RequestMapping maps URLs, @RequestParam extracts query parameters, others handle HTTP methods or errors.
  3. Final Answer:

    It extracts query string parameters from the URL and passes them to the method. -> Option B
  4. Quick Check:

    @RequestParam reads query parameters = A [OK]
Hint: Remember: @RequestParam reads URL query values [OK]
Common Mistakes:
  • Confusing @RequestParam with @PathVariable
  • Thinking it maps URLs instead of parameters
  • Assuming it handles HTTP methods
2. Which of the following is the correct syntax to make a query parameter optional with a default value using @RequestParam?
easy
A. @RequestParam(value = "id", required = true, defaultValue = "0") int id
B. @RequestParam(name = "id", optional = true, default = 0) int id
C. @RequestParam(name = "id", required = false, defaultValue = "0") int id
D. @RequestParam("id", default = 0) int id

Solution

  1. Step 1: Identify correct attributes for optional param

    To make a parameter optional, use required = false and provide defaultValue as a string.
  2. Step 2: Check syntax correctness

    @RequestParam(name = "id", required = false, defaultValue = "0") int id uses required = false and defaultValue = "0" correctly; others use invalid attributes or miss required parts.
  3. Final Answer:

    @RequestParam(name = "id", required = false, defaultValue = "0") int id -> Option C
  4. Quick Check:

    Optional param syntax = A [OK]
Hint: Use required=false and defaultValue="value" for optional params [OK]
Common Mistakes:
  • Using 'optional' instead of 'required'
  • Setting defaultValue as a number instead of string
  • Omitting required attribute
3. Given the controller method:
@GetMapping("/greet")
public String greet(@RequestParam String name) {
    return "Hello, " + name + "!";
}

What will be the output when accessing /greet?name=Anna?
medium
A. Hello, Anna!
B. Hello, null!
C. Hello, !
D. Error: Missing required parameter

Solution

  1. Step 1: Understand @RequestParam behavior

    The method expects a query parameter 'name' which is required by default.
  2. Step 2: Check the URL query string

    The URL provides ?name=Anna, so the method receives "Anna" as the parameter.
  3. Final Answer:

    Hello, Anna! -> Option A
  4. Quick Check:

    Query param 'name' = Anna, output greeting = D [OK]
Hint: If param is required and given, output uses it directly [OK]
Common Mistakes:
  • Assuming missing param causes null instead of error
  • Confusing query string with path variable
  • Expecting default value without setting one
4. Identify the error in this Spring Boot controller method:
@GetMapping("/search")
public String search(@RequestParam int page) {
    return "Page: " + page;
}

What happens if the URL /search is accessed without any query parameters?
medium
A. The method returns "Page: 0"
B. The method returns "Page: null"
C. The method returns an empty string
D. The method throws a 400 Bad Request error

Solution

  1. Step 1: Check @RequestParam default behavior

    By default, @RequestParam is required. Since 'page' is primitive int, it cannot be null.
  2. Step 2: Analyze missing parameter effect

    Accessing /search without 'page' causes Spring to reject the request with 400 Bad Request due to missing required param.
  3. Final Answer:

    The method throws a 400 Bad Request error -> Option D
  4. Quick Check:

    Missing required param causes 400 error = C [OK]
Hint: Required params missing cause 400 error, primitives can't be null [OK]
Common Mistakes:
  • Assuming default 0 for missing int param
  • Expecting null for primitive types
  • Thinking method returns empty string on error
5. You want to create a Spring Boot controller method that accepts an optional query parameter sort with default value "asc" and a required parameter page. Which method signature is correct?
hard
A. @GetMapping("/items") public String items(@RequestParam(required = false, defaultValue = "asc") String sort, @RequestParam int page) { ... }
B. @GetMapping("/items") public String items(@RequestParam(required = true, defaultValue = "asc") String sort, @RequestParam int page) { ... }
C. @GetMapping("/items") public String items(@RequestParam(required = false) String sort, @RequestParam(required = true) int page) { ... }
D. @GetMapping("/items") public String items(@RequestParam String sort = "asc", @RequestParam int page) { ... }

Solution

  1. Step 1: Set optional param with default value

    Use required = false and defaultValue = "asc" for the optional 'sort' parameter.
  2. Step 2: Ensure required param is declared correctly

    The 'page' parameter is required by default, so just @RequestParam int page is enough.
  3. Step 3: Evaluate each option

    @GetMapping("/items") public String items(@RequestParam(required = false, defaultValue = "asc") String sort, @RequestParam int page) { ... } correctly combines required=false and defaultValue for 'sort' and required 'page'. Incorrect options include one with required=true and defaultValue (still required on missing), one with required=false without defaultValue (null on missing), and one with invalid syntax.
  4. Final Answer:

    @GetMapping("/items") public String items(@RequestParam(required = false, defaultValue = "asc") String sort, @RequestParam int page) { ... } -> Option A
  5. Quick Check:

    Optional with default + required param = B [OK]
Hint: Use required=false with defaultValue for optional params [OK]
Common Mistakes:
  • Omitting required=false for optional param
  • Trying to assign default value in method signature
  • Forgetting defaultValue causes errors
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