Dynamic Parameter Handling in Spring Boot Backend Development Quiz

Explore the essential concepts of managing dynamic parameters in Spring Boot backend applications. This quiz covers key annotations, parameter extraction methods, and best practices for handling various types of request parameters efficiently and securely.

  1. Annotation for Request Parameters

    Which annotation is commonly used in Spring Boot to bind a query parameter from the URL to a method argument?

    1. @RequestParam
    2. @PathVariable
    3. @RequestBody
    4. @Autowired

    Explanation: @RequestParam is used to extract and bind a query parameter from the request URL to a controller method argument. @PathVariable is used for extracting template variables from the URI, not query parameters. @RequestBody binds request data to an object, and @Autowired is used for dependency injection, not parameter handling.

  2. Handling Path Variables

    If you want to capture a value from the URL path like /users/5, which annotation should you use in Spring Boot?

    1. @RequestParam
    2. @PathVariable
    3. @Value
    4. @Param

    Explanation: @PathVariable binds values from the URI path directly to method arguments, making it ideal for capturing things like IDs or names from the endpoint. @RequestParam is for query parameters, @Value is for reading configuration values, and @Param is not used for extracting path variables in controllers.

  3. Default Values with Request Parameters

    How can you provide a default value for a query parameter using @RequestParam in Spring Boot?

    1. By using defaultValue attribute
    2. By setting required=false
    3. By using @Default
    4. By declaring as final

    Explanation: The defaultValue attribute within @RequestParam allows you to specify a fallback value if the query parameter is absent in the request. Setting required=false makes the parameter optional but does not assign a default. @Default is not a valid annotation, and declaring the method argument as final does not affect the default value.

  4. Handling Optional Query Parameters

    Which approach allows a Spring Boot controller to handle a query parameter that may not always be present?

    1. Set required=false in @RequestParam
    2. Use @PathVariable instead
    3. Declare parameter as static
    4. Set value to zero

    Explanation: Setting required=false in the @RequestParam annotation makes the query parameter optional, allowing requests to succeed even if the parameter is missing. Using @PathVariable does not deal with query parameters, declaring a parameter as static is incorrect, and setting a value to zero does not handle parameter presence.

  5. Extracting All Query Parameters

    In Spring Boot, which type can you use to collect all query parameters in a controller method?

    1. Map<String, String>
    2. List<String>
    3. Set<String>
    4. Queue<String>

    Explanation: A Map<String, String> can be used with @RequestParam to capture all query parameters, pairing parameter names with their values. A List<String> or Set<String> does not associate names with values, and Queue<String> is not used for parameter extraction.

  6. Dynamic REST Endpoints

    What is the primary benefit of using @PathVariable in dynamic REST endpoints?

    1. It allows endpoints to handle variable parts of the URL path
    2. It automatically validates user input
    3. It binds JSON payloads to objects
    4. It generates documentation automatically

    Explanation: @PathVariable enables a controller to extract dynamic segments from the path, allowing endpoints like /users/5 to handle different values. It does not validate user input, bind JSON, or auto-generate documentation, which are functions of other tools or annotations.

  7. Arrays as Query Parameters

    How can a Spring Boot method receive multiple values for the same query parameter key (e.g., ids=1&ids=2)?

    1. By using an array or List as the parameter type
    2. By using @PathVariable with an array
    3. By splitting a string manually
    4. By using @Autowired

    Explanation: Using an array or List as the method argument type allows the framework to automatically bind repeated parameters like ids=1&ids=2 to a Java collection. @PathVariable is not for query parameters, splitting strings manually is unnecessary, and @Autowired is unrelated.

  8. Handling Dynamic Filters in Search APIs

    Which is a recommended way to handle multiple optional search filters in a GET endpoint in Spring Boot?

    1. Use multiple @RequestParam annotations with required=false
    2. Use @RequestBody to receive a map
    3. Rely exclusively on @PathVariable
    4. Ignore missing parameters completely

    Explanation: Multiple optional filters can be implemented using @RequestParam with required=false, enabling users to supply any combination of values. @RequestBody with a map is more suited to POST requests, @PathVariable is for mandatory path elements, and ignoring missing parameters may cause ambiguity.

  9. @RequestBody vs @RequestParam

    When should you use @RequestBody instead of @RequestParam for parameter binding in a Spring Boot controller?

    1. When processing JSON or form data in the request body
    2. When capturing URL template variables
    3. When reading a single query parameter
    4. When injecting dependencies

    Explanation: @RequestBody is used for binding the entire body of a request, such as JSON payloads or form data, to a Java object. URL template variables are handled by @PathVariable, single query parameters by @RequestParam, and dependency injection does not involve parameter binding.

  10. Type Conversion for Parameters

    What happens if you use @RequestParam for a parameter typed as int, but the provided value cannot be converted?

    1. A conversion error is thrown
    2. The value defaults to zero
    3. The value is ignored silently
    4. It is converted to a string

    Explanation: A conversion error will be thrown if the value cannot be converted to the declared type, resulting in a runtime exception. The value does not silently default to zero, is not ignored, and is not coerced into a string for an int parameter.

  11. URL-Encoded Parameters

    How does Spring Boot handle URL-encoded special characters (like spaces or symbols) in query parameters?

    1. It automatically decodes them before binding
    2. It stores them as raw encoded values
    3. It ignores special characters
    4. It replaces them with underscores

    Explanation: Spring Boot automatically decodes URL-encoded special characters before binding them to method arguments. It does not store raw encoded values, ignore characters, or replace them with underscores, ensuring the developer receives the correct data.

  12. Combining @RequestParam and @PathVariable

    Can you use @RequestParam and @PathVariable in the same Spring Boot controller method, and what is a common use-case?

    1. Yes, for endpoints like /users/3?active=true
    2. No, they are mutually exclusive
    3. Yes, but only in GET methods
    4. No, unless using POST requests

    Explanation: You can use both annotations in a single method to extract data from both the URL path and query string, such as retrieving a user by ID and filtering by status. They are not mutually exclusive, are not limited to GET methods, and do not require POST requests.

  13. Handling Missing Required Path Variables

    What happens if a required @PathVariable is missing from a request to a Spring Boot endpoint?

    1. The request returns an error
    2. The variable is set to a random value
    3. The endpoint proceeds with a null value
    4. A default value is used automatically

    Explanation: Spring Boot will return an error, typically a 404, if a required path variable is missing. It does not assign a random value, proceed with null, or use an automatic default, maintaining strict mapping requirements.

  14. Using Maps with @RequestParam

    What is a benefit of using Map<String,String> with @RequestParam in controller methods?

    1. It collects all query parameters dynamically
    2. It restricts access to only one parameter
    3. It disables type checking
    4. It is required for POST requests

    Explanation: Using a Map with @RequestParam collects all query parameters into key-value pairs without explicitly naming them. It does not restrict access, disable type checking, or is required for POST requests, which are incorrect uses.

  15. Parameter Validation

    Which built-in annotation can you use for validating query parameter values, such as ensuring a number is positive?

    1. @Positive
    2. @NotFound
    3. @Validated
    4. @Autowired

    Explanation: @Positive is a validation annotation ensuring numeric parameters are positive. @NotFound is not a validation annotation, @Validated enables validation but does not specify rules, and @Autowired is for dependency injection, not validation.

  16. Required Parameters Default Behavior

    By default, what happens if a required @RequestParam is missing from the request in a Spring Boot controller?

    1. A 400 error is returned
    2. A default value of empty string is used
    3. The parameter is considered null
    4. No error occurs

    Explanation: If a required @RequestParam is not provided and no defaultValue is set, Spring Boot returns a 400 error, signaling a bad request. An empty string is not automatically used; the parameter is not set to null, and no error is not correct behavior for required parameters.