Bird
0
0

What will be the output of the following FastAPI code when accessing /items/3?

medium📝 Predict Output Q5 of 15
FastAPI - Dependency Injection
What will be the output of the following FastAPI code when accessing /items/3?
from fastapi import FastAPI, Depends

app = FastAPI()

def common_parameters(q: str = None):
    return {"q": q}

@app.get("/items/{item_id}")
def read_item(item_id: int, commons: dict = Depends(common_parameters)):
    return {"item_id": item_id, **commons}
AError: missing query parameter q
B{"item_id": 3, "q": ""}
C{"item_id": 3}
D{"item_id": 3, "q": null}
Step-by-Step Solution
Solution:
  1. Step 1: Understand default query parameter behavior

    Parameter q has a default None, so if not provided in query, it will be None.
  2. Step 2: Analyze returned dictionary

    The returned dictionary merges item_id and commons, so q will be null (None in Python).
  3. Final Answer:

    {"item_id": 3, "q": null} -> Option D
  4. Quick Check:

    Default query param None = null in JSON [OK]
Quick Trick: Default None query params become null in JSON output [OK]
Common Mistakes:
MISTAKES
  • Expecting empty string instead of null
  • Missing query param causes error
  • Ignoring merged dictionary keys

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes