Bird
0
0

Consider this method:

medium📝 component behavior Q5 of 15
Spring Boot - REST Controllers
Consider this method:
@GetMapping("/items")
public String getItem(@RequestParam(defaultValue = "1") int page) {
    return "Page number: " + page;
}

What will be the output when the URL is /items without any query parameters?
APage number: null
BPage number: 0
CError: Missing required parameter
DPage number: 1
Step-by-Step Solution
Solution:
  1. Step 1: Check defaultValue usage

    Since no query parameter is provided, the defaultValue "1" is used and converted to int 1.
  2. Step 2: Understand method return

    The method returns "Page number: 1" as the output string.
  3. Final Answer:

    Page number: 1 -> Option D
  4. Quick Check:

    Default value used when param missing [OK]
Quick Trick: defaultValue sets fallback when param missing [OK]
Common Mistakes:
  • Expecting error when param is missing with defaultValue
  • Assuming defaultValue is ignored
  • Confusing defaultValue type conversion

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes