0
0
FastAPIframework~20 mins

Basic query parameter declaration 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 accessing the endpoint with ?q=hello?

Consider this FastAPI endpoint:

from fastapi import FastAPI

app = FastAPI()

@app.get("/items/")
async def read_items(q: str):
    return {"query": q}

What will the response be when you visit /items/?q=hello?

FastAPI
from fastapi import FastAPI

app = FastAPI()

@app.get("/items/")
async def read_items(q: str):
    return {"query": q}
A{"query": null}
B{"query": "hello"}
C{"error": "Missing query parameter"}
D{"query": "q=hello"}
Attempts:
2 left
💡 Hint

Query parameters are passed after the ? in the URL and matched by name.

📝 Syntax
intermediate
2:00remaining
Which option correctly declares an optional query parameter with a default value?

In FastAPI, how do you declare a query parameter q that is optional and defaults to None?

Aasync def read_items(q: Optional[str]):
Basync def read_items(q: str):
Casync def read_items(q: str = None):
Dasync def read_items(q: str = ''):
Attempts:
2 left
💡 Hint

Optional parameters need a default value in Python function definitions.

state_output
advanced
2:00remaining
What is the output when no query parameter is provided?

Given this FastAPI endpoint:

from fastapi import FastAPI

app = FastAPI()

@app.get("/search/")
async def search_items(q: str = "default"):
    return {"query": q}

What will be the response when you visit /search/ without any query parameters?

FastAPI
from fastapi import FastAPI

app = FastAPI()

@app.get("/search/")
async def search_items(q: str = "default"):
    return {"query": q}
A{"query": "default"}
B{"error": "Missing query parameter"}
C{"query": null}
D{"query": ""}
Attempts:
2 left
💡 Hint

Default values are used when no value is provided.

🔧 Debug
advanced
2:00remaining
Which option causes a runtime error when calling the endpoint?

Consider these FastAPI endpoint definitions. Which one will cause a runtime error when accessed without query parameters?

Aasync def get_data(q: Optional[str]): return {"q": q}
Basync def get_data(q: str = None): return {"q": q}
Casync def get_data(q: str = ""): return {"q": q}
Dasync def get_data(q: str): return {"q": q}
Attempts:
2 left
💡 Hint

Check if the parameter has a default value or is optional.

🧠 Conceptual
expert
3:00remaining
How does FastAPI handle multiple query parameters with the same name?

Given this endpoint:

from fastapi import FastAPI
from typing import List

app = FastAPI()

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

What will be the output when the URL is /items/?q=foo&q=bar&q=baz?

FastAPI
from fastapi import FastAPI
from typing import List

app = FastAPI()

@app.get("/items/")
async def read_items(q: List[str] = []):
    return {"queries": q}
A{"queries": []}
B{"queries": "foo,bar,baz"}
C{"queries": ["foo"]}
D{"queries": ["foo", "bar", "baz"]}
Attempts:
2 left
💡 Hint

FastAPI can collect multiple query parameters with the same name into a list.