Bird
0
0

Consider this FastAPI snippet:

medium📝 component behavior Q4 of 15
FastAPI - Dependency Injection
Consider this FastAPI snippet:
from fastapi import FastAPI, Depends
app = FastAPI()
def provide_value():
    return 100
@app.get("/value")
async def get_value(val: int = Depends(provide_value)):
    return {"value": val}

What response will the endpoint return when accessed?
A{"value": "provide_value"}
B{"value": 100}
C{"value": null}
DAn error due to missing parameter
Step-by-Step Solution
Solution:
  1. Step 1: Understand the dependency

    The function provide_value returns the integer 100.
  2. Step 2: Dependency injection in endpoint

    The endpoint get_value uses Depends(provide_value) to inject the return value of provide_value into the parameter val.
  3. Step 3: Endpoint response

    The endpoint returns a JSON with key "value" and the injected integer 100.
  4. Final Answer:

    {"value": 100} -> Option B
  5. Quick Check:

    Dependency returns 100, injected correctly [OK]
Quick Trick: Dependency returns value injected as parameter [OK]
Common Mistakes:
MISTAKES
  • Assuming the function name is returned instead of its output
  • Expecting a null or error response
  • Confusing parameter injection with direct call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes