0
0
FastAPIframework~20 mins

Why advanced patterns solve real problems in FastAPI - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI Advanced Patterns 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 injection example?
Consider this FastAPI code using dependency injection. What will the endpoint return when accessed?
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 {"number": num}
A{"number": null}
B{"number": "42"}
C{"num": 42}
D{"number": 42}
Attempts:
2 left
💡 Hint
Think about what the dependency function returns and how FastAPI injects it.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a FastAPI path operation with a query parameter?
Select the code snippet that correctly defines a GET endpoint with a required query parameter named 'q' of type string.
A
from fastapi import FastAPI
app = FastAPI()

@app.get("/search")
async def search(q: str):
    return {"query": q}
B
from fastapi import FastAPI
app = FastAPI()

@app.get("/search")
async def search(q):
    return {"query": q}
C
from fastapi import FastAPI
app = FastAPI()

@app.get("/search")
async def search(q: int):
    return {"query": q}
D
from fastapi import FastAPI
app = FastAPI()

@app.get("/search")
async def search():
    return {"query": q}
Attempts:
2 left
💡 Hint
Query parameters need type annotations to work properly in FastAPI.
🔧 Debug
advanced
2:00remaining
Why does this FastAPI endpoint raise a validation error?
Examine the code and choose the reason why a POST request with JSON {"name": "Alice"} causes a validation error.
FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    name: str
    price: float

@app.post("/items")
async def create_item(item: Item):
    return item
AThe 'name' field is not a string, causing a type error.
BThe endpoint method should be GET, not POST.
CThe JSON is missing the required 'price' field, causing a validation error.
DThe Pydantic model is missing a default value for 'price', causing a syntax error.
Attempts:
2 left
💡 Hint
Check the required fields in the Pydantic model and the JSON sent.
state_output
advanced
2:00remaining
What is the output after calling this FastAPI endpoint twice?
Given this code, what will be the response of the second call to /counter?
FastAPI
from fastapi import FastAPI

app = FastAPI()
counter = 0

@app.get("/counter")
async def get_counter():
    global counter
    counter += 1
    return {"count": counter}
A{"count": 2}
B{"count": 0}
C{"count": 1}
DRaises a NameError due to global variable usage
Attempts:
2 left
💡 Hint
Think about how the global variable changes with each call.
🧠 Conceptual
expert
3:00remaining
Why do advanced FastAPI patterns like dependency injection improve real-world applications?
Choose the best explanation for why using advanced patterns such as dependency injection helps in building scalable FastAPI apps.
AThey reduce the number of lines of code by automatically generating endpoints.
BThey allow separating concerns, making code easier to test, maintain, and reuse across endpoints.
CThey force all data to be stored in global variables for faster access.
DThey eliminate the need for type annotations, simplifying function signatures.
Attempts:
2 left
💡 Hint
Think about how dependency injection affects code organization and testing.