0
0
FastAPIframework~20 mins

Multiple query parameters in FastAPI - Practice Problems & Coding Challenges

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