Bird
Raised Fist0
FastAPIframework~20 mins

Multiple query parameters in FastAPI - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
FastAPI 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 multiple query parameters are passed?

Consider this FastAPI endpoint:

from fastapi import FastAPI
app = FastAPI()

@app.get("/items/")
async def read_items(q: str, limit: int = 10):
    return {"q": q, "limit": limit}

What will be the JSON response if the URL is /items/?q=book&limit=5?

FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/items/")
async def read_items(q: str, limit: int = 10):
    return {"q": q, "limit": limit}
A{"q": null, "limit": 5}
B{"q": "book", "limit": 5}
C{"q": "book", "limit": 10}
DHTTP 422 Unprocessable Entity error
Attempts:
2 left
💡 Hint

Query parameters in FastAPI are matched by name and type. Default values are used only if the parameter is missing.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines multiple query parameters with defaults?

Which FastAPI function signature correctly defines two optional query parameters category (string) and page (integer) with default values?

Aasync def get_items(category: str = None, page: int = 1):
Basync def get_items(category: str = "", page: int):
Casync def get_items(category: str, page: int = 1):
Dasync def get_items(category: str = None, page: int = None):
Attempts:
2 left
💡 Hint

Optional query parameters must have default values. Non-optional parameters without defaults are required.

🔧 Debug
advanced
2:00remaining
Why does this FastAPI endpoint raise a validation error?

Given this endpoint:

from fastapi import FastAPI
app = FastAPI()

@app.get("/search")
async def search_items(q: str, limit: int = "10"):
    return {"q": q, "limit": limit}

Why does calling /search?q=phone cause a validation error?

FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/search")
async def search_items(q: str, limit: int = "10"):
    return {"q": q, "limit": limit}
ABecause limit is not declared as Optional[int]
BBecause q is missing a default value
CBecause the endpoint path is missing a trailing slash
DBecause limit has a default value as string "10" but should be int
Attempts:
2 left
💡 Hint

Check the type of default values compared to the declared parameter type.

❓ state_output
advanced
2:00remaining
What is the value of 'tags' after this request?

Consider this FastAPI endpoint:

from fastapi import FastAPI
from typing import List
app = FastAPI()

@app.get("/filter")
async def filter_items(tags: List[str] = []):
    return {"tags": tags}

What will be the output if the URL is /filter?tags=red&tags=blue?

FastAPI
from fastapi import FastAPI
from typing import List
app = FastAPI()

@app.get("/filter")
async def filter_items(tags: List[str] = []):
    return {"tags": tags}
A{"tags": ["red", "blue"]}
B{"tags": ["redblue"]}
C{"tags": []}
DHTTP 422 Unprocessable Entity error
Attempts:
2 left
💡 Hint

Multiple query parameters with the same name are parsed as a list.

🧠 Conceptual
expert
2:00remaining
Which option correctly explains how FastAPI handles multiple query parameters with the same name?

When a FastAPI endpoint declares a parameter as tags: List[str], and the request URL contains multiple tags query parameters, how does FastAPI process them?

AFastAPI only takes the first 'tags' query parameter and ignores the rest.
BFastAPI concatenates all 'tags' query parameters into a single string separated by commas.
CFastAPI collects all query parameters named 'tags' into a Python list and passes it to the endpoint parameter.
DFastAPI raises a validation error if multiple 'tags' parameters are present.
Attempts:
2 left
💡 Hint

Think about how lists in function parameters relate to repeated query parameters.

Practice

(1/5)
1. What is the main purpose of using multiple query parameters in a FastAPI endpoint?
easy
A. To accept several inputs from the URL to customize the response
B. To define multiple routes in one function
C. To handle only POST request data
D. To create database models automatically

Solution

  1. Step 1: Understand query parameters in FastAPI

    Query parameters allow passing data in the URL after a question mark, like ?name=John.
  2. Step 2: Purpose of multiple query parameters

    Using multiple query parameters lets the API accept many inputs to customize what it returns.
  3. Final Answer:

    To accept several inputs from the URL to customize the response -> Option A
  4. Quick Check:

    Multiple query parameters = multiple inputs [OK]
Hint: Multiple query parameters let you send many inputs in URL [OK]
Common Mistakes:
  • Confusing query parameters with request body
  • Thinking multiple query parameters define routes
  • Assuming query parameters only work with POST
2. Which of the following is the correct way to declare two query parameters name (string) and age (integer) in a FastAPI path operation function?
easy
A. def read_user(name: str = Query(...), age: int = Query(...)):
B. def read_user(name: str, age: int = None):
C. def read_user(name: int, age: str):
D. def read_user(name, age):

Solution

  1. Step 1: Understand FastAPI query parameter declaration

    Using Query(...) marks parameters as required query parameters with type hints.
  2. Step 2: Check parameter types and usage

    Parameters must have correct types and use Query(...) to be recognized as query parameters.
  3. Final Answer:

    def read_user(name: str = Query(...), age: int = Query(...)): -> Option A
  4. Quick Check:

    Use Query(...) with correct types for query parameters [OK]
Hint: Use Query(...) with type hints for required query parameters [OK]
Common Mistakes:
  • Swapping types of parameters
  • Not using Query for required parameters
  • Omitting type hints
3. Given this FastAPI endpoint code:
from fastapi import FastAPI, Query
from typing import List

app = FastAPI()

@app.get('/items/')
async def read_items(q: List[str] = Query([])):
    return {"q": q}

What will be the output if the URL is /items/?q=apple&q=banana?
medium
A. {"q": ["apple"]}
B. {"q": "apple,banana"}
C. {"q": ["apple", "banana"]}
D. {"q": []}

Solution

  1. Step 1: Understand List query parameters in FastAPI

    Using List[str] with Query([]) allows multiple values for the same query key to be collected into a list.
  2. Step 2: Analyze the URL query parameters

    The URL has q=apple and q=banana, so FastAPI collects both into a list.
  3. Final Answer:

    {"q": ["apple", "banana"]} -> Option C
  4. Quick Check:

    Multiple query keys = list of values [OK]
Hint: Multiple same keys in URL become list in FastAPI [OK]
Common Mistakes:
  • Expecting a single string with commas
  • Getting only the first value
  • Getting an empty list
4. What is wrong with this FastAPI endpoint code that tries to accept two query parameters name and age?:
from fastapi import FastAPI
app = FastAPI()

@app.get('/user/')
def get_user(name: str, age: int = None):
    return {"name": name, "age": age}
medium
A. The route path should not end with a slash
B. The function must be async
C. Missing Query() for both parameters
D. The parameter age should use Optional[int] for optional query

Solution

  1. Step 1: Check optional query parameter typing

    In FastAPI, optional parameters should use Optional[type] or type | None to mark them optional properly.
  2. Step 2: Analyze age: int = None

    Just setting default to None is not enough; the type hint should be Optional[int] or int | None for FastAPI to treat it as optional.
  3. Final Answer:

    The parameter age should use Optional[int] for optional query -> Option D
  4. Quick Check:

    Optional query params need Optional[type] or type | None [OK]
Hint: Use Optional[int] or int | None for optional query params [OK]
Common Mistakes:
  • Assuming default None alone makes param optional
  • Forgetting to import Optional
  • Thinking async is required for all endpoints
5. You want to create a FastAPI endpoint that accepts multiple optional query parameters: tags (list of strings), limit (integer, default 10), and search (optional string). Which of the following function signatures correctly implements this?
hard
A. def get_items(tags: List[str] = Query([]), limit: int = 10, search: str = None):
B. def get_items(tags: List[str] = Query([]), limit: int = Query(10), search: Optional[str] = Query(None)):
C. def get_items(tags: List[str] = [], limit: int = 10, search: str = ''):
D. def get_items(tags: List[str], limit: int = Query(10), search: Optional[str] = Query(None)):

Solution

  1. Step 1: Use Query() for query parameters with defaults

    FastAPI requires using Query() to specify default values and metadata for query parameters.
  2. Step 2: Correctly type optional parameters

    Use Optional[str] or str | None with Query(None) for optional string parameters.
  3. Step 3: Check all parameters in def get_items(tags: List[str] = Query([]), limit: int = Query(10), search: Optional[str] = Query(None)):

    def get_items(tags: List[str] = Query([]), limit: int = Query(10), search: Optional[str] = Query(None)): uses Query([]) for list default, Query(10) for limit default, and Optional[str] = Query(None) for optional search, which is correct.
  4. Final Answer:

    def get_items(tags: List[str] = Query([]), limit: int = Query(10), search: Optional[str] = Query(None)): -> Option B
  5. Quick Check:

    Use Query() with correct types and defaults for multiple optional queries [OK]
Hint: Use Query() with defaults and Optional for optional query params [OK]
Common Mistakes:
  • Using mutable default [] without Query()
  • Not marking optional string with Optional
  • Omitting Query() for default values
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