0
0
FastAPIframework~20 mins

Depends function basics in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Depends Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this FastAPI endpoint using Depends?

Consider this FastAPI code snippet:

from fastapi import FastAPI, Depends

app = FastAPI()

def get_query():
    return "query_value"

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

What will be the JSON response when calling /items?

FastAPI
from fastapi import FastAPI, Depends

app = FastAPI()

def get_query():
    return "query_value"

@app.get("/items")
async def read_items(q: str = Depends(get_query)):
    return {"q": q}
A{"q": "query_value"}
B{"q": null}
CHTTP 422 Unprocessable Entity error
D{"q": ""}
Attempts:
2 left
💡 Hint

Think about what the Depends function does when used as a default value.

state_output
intermediate
2:00remaining
What is the value of 'user' after this dependency injection?

Given this FastAPI code:

from fastapi import Depends, FastAPI

app = FastAPI()

def get_user():
    return {"name": "Alice"}

@app.get("/profile")
async def profile(user: dict = Depends(get_user)):
    return user

What will be the output JSON when calling /profile?

FastAPI
from fastapi import Depends, FastAPI

app = FastAPI()

def get_user():
    return {"name": "Alice"}

@app.get("/profile")
async def profile(user: dict = Depends(get_user)):
    return user
A{}
B{"user": "Alice"}
C{"name": "Alice"}
DHTTP 500 Internal Server Error
Attempts:
2 left
💡 Hint

Check what the dependency function returns and how it is used in the endpoint.

📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in Depends usage?

Identify which code snippet will cause a syntax error when using Depends in FastAPI.

A
async def endpoint(dep Depends(get_dep)):
    return dep
B
async def endpoint(dep: str = Depends(get_dep)):
    return dep
C
async def endpoint(dep=Depends(get_dep)):
    return dep
D
ped nruter    
:))ped_teg(sdnepeD = rts :ped(tniopdne fed cnysa
Attempts:
2 left
💡 Hint

Look carefully at the function parameter syntax.

🔧 Debug
advanced
2:00remaining
Why does this FastAPI endpoint raise a runtime error?

Examine this code:

from fastapi import FastAPI, Depends

app = FastAPI()

def get_number():
    return 42

@app.get("/number")
async def read_number(num: int = Depends(get_number)):
    return {"num": num + 1}

When calling /number, a runtime error occurs. Why?

FastAPI
from fastapi import FastAPI, Depends

app = FastAPI()

def get_number():
    return 42

@app.get("/number")
async def read_number(num: int = Depends(get_number)):
    return {"num": num + 1}
ABecause get_number returns int but FastAPI expects a str for query parameters
BNo runtime error occurs; the output is {"num": 43}
CBecause num is None and cannot be added to 1
DBecause Depends does not support functions returning int
Attempts:
2 left
💡 Hint

Consider the type returned by the dependency and how it is used.

🧠 Conceptual
expert
2:00remaining
Which statement best describes how Depends works in FastAPI?

Choose the most accurate description of the Depends function behavior in FastAPI.

ADepends automatically converts all dependency return values to strings before injection.
BDepends caches the dependency function result globally and reuses it for all requests.
CDepends requires the dependency function to be async and returns a coroutine object.
DDepends calls the dependency function once per request and injects its return value into the endpoint parameter.
Attempts:
2 left
💡 Hint

Think about how dependencies are executed and their scope.