0
0
FastAPIframework~20 mins

Optional query parameters in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI Optional 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 no query parameter is provided?
Consider this FastAPI endpoint that uses an optional query parameter with a default value. What will be the response if the client calls the endpoint without any query parameters?
FastAPI
from fastapi import FastAPI
from typing import Optional

app = FastAPI()

@app.get('/items/')
async def read_items(q: Optional[str] = None):
    if q:
        return {'query': q}
    return {'query': 'No query provided'}
A{"query": "No query provided"}
B{"query": null}
C404 Not Found error
D500 Internal Server Error
Attempts:
2 left
💡 Hint
Think about what happens when the optional parameter is not sent by the client.
📝 Syntax
intermediate
2:00remaining
Which option correctly declares an optional query parameter with a default value?
You want to declare an optional query parameter named 'page' that defaults to 1. Which of the following FastAPI endpoint parameter declarations is correct?
Aasync def read_items(page: Optional[int] = 1):
Basync def read_items(page: int = 1):
Casync def read_items(page: int):
Dasync def read_items(page: Optional[int]):
Attempts:
2 left
💡 Hint
Remember how to set default values for parameters in Python functions.
state_output
advanced
2:00remaining
What is the response when multiple optional query parameters are missing?
Given this FastAPI endpoint with two optional query parameters, what will be the JSON response if the client calls the endpoint without any query parameters?
FastAPI
from fastapi import FastAPI
from typing import Optional

app = FastAPI()

@app.get('/search/')
async def search(q: Optional[str] = None, limit: Optional[int] = 10):
    return {'q': q, 'limit': limit}
A{"q": null, "limit": 10}
B{"q": "", "limit": 0}
C{"q": null, "limit": null}
D422 Unprocessable Entity error
Attempts:
2 left
💡 Hint
Check the default values assigned to the parameters.
🔧 Debug
advanced
2:00remaining
Why does this FastAPI endpoint raise a validation error?
Examine the following FastAPI endpoint. Why does it raise a 422 validation error when called without query parameters?
FastAPI
from fastapi import FastAPI
from typing import Optional

app = FastAPI()

@app.get('/items/')
async def read_items(q: Optional[str]):
    return {'query': q}
ABecause the return statement is missing a default value.
BBecause Optional[str] is not allowed in FastAPI parameters.
CBecause the endpoint must have at least one required parameter.
DBecause 'q' is Optional but has no default value, so it is required.
Attempts:
2 left
💡 Hint
Think about how Python treats parameters without default values.
🧠 Conceptual
expert
2:00remaining
How does FastAPI handle optional query parameters with default values in OpenAPI docs?
When you declare an optional query parameter with a default value in FastAPI, how does it appear in the generated OpenAPI documentation?
AThe parameter is marked as required with no default value shown.
BThe parameter is hidden from the OpenAPI docs.
CThe parameter is marked as optional with the default value shown in the docs.
DThe parameter is shown as required but with the default value.
Attempts:
2 left
💡 Hint
Think about how default values affect API documentation.