0
0
FastAPIframework~20 mins

Dependencies with parameters in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI Dependency Master
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 dependency with parameter?

Consider this FastAPI dependency that takes a parameter and returns a greeting message. What will be the response when accessing /hello?name=Alice?

FastAPI
from fastapi import FastAPI, Depends

app = FastAPI()

def greet(name: str):
    return f"Hello, {name}!"

@app.get("/hello")
async def hello(message: str = Depends(greet)):
    return {"message": message}
A{"message": "Hello, Alice!"}
B{"message": "Hello, name!"}
C{"message": "Hello!"}
D{"message": "Hello, None!"}
Attempts:
2 left
💡 Hint

Think about how the Depends function injects the parameter from the query string.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a FastAPI dependency with a default parameter value?

Choose the correct way to define a dependency function that takes an optional parameter lang with default value "en".

A
def get_language(lang: str):
    if not lang:
        lang = "en"
    return lang
B
def get_language(lang: str = "en"):
    return lang
C
def get_language(lang: str = Query("en")):
    return lang
D
def get_language(lang: str = Depends("en")):
    return lang
Attempts:
2 left
💡 Hint

Remember to use FastAPI's Query for default query parameters in dependencies.

🔧 Debug
advanced
2:00remaining
Why does this FastAPI dependency with parameter cause a runtime error?

Examine the code below. Why does calling /items cause a runtime error?

FastAPI
from fastapi import FastAPI, Depends

app = FastAPI()

def get_multiplier(factor: int):
    return lambda x: x * factor

@app.get("/items")
async def read_items(multiplier = Depends(get_multiplier)):
    return {"result": multiplier(5)}
ARuntimeError because get_multiplier requires a parameter but FastAPI cannot provide it automatically
BTypeError because multiplier is not callable
CKeyError because 'factor' is missing in request
DNo error, returns {"result": 25}
Attempts:
2 left
💡 Hint

Think about how FastAPI injects parameters into dependencies.

🧠 Conceptual
advanced
2:00remaining
How does FastAPI handle dependencies with parameters that have default values?

Which statement best describes how FastAPI resolves dependencies with parameters that have default values?

AFastAPI raises an error if the parameter has a default value
BFastAPI always requires the parameter to be provided in the request, ignoring defaults
CFastAPI ignores default values and sets parameters to None
DFastAPI uses the default value if the parameter is not provided in the request
Attempts:
2 left
💡 Hint

Think about how query parameters with defaults behave in FastAPI.

state_output
expert
2:00remaining
What is the output of this FastAPI dependency chain with parameters?

Given the following code, what is the JSON response when accessing /compute?value=3?

FastAPI
from fastapi import FastAPI, Depends, Query

app = FastAPI()

def double(value: int = Query(...)):
    return value * 2

def add_five(number: int = Depends(double)):
    return number + 5

@app.get("/compute")
async def compute(result: int = Depends(add_five)):
    return {"result": result}
A{"result": 8}
B{"result": 11}
C{"result": 16}
D{"result": 10}
Attempts:
2 left
💡 Hint

Trace the dependency calls and their returned values step-by-step.