0
0
FastAPIframework~20 mins

Route decorator syntax in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI Route 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 route?
Consider this FastAPI route definition. What will the server respond with when a GET request is made to /hello?
FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get('/hello')
async def say_hello():
    return {'message': 'Hello, FastAPI!'}
AA 404 Not Found error
BA plain text response with 'Hello, FastAPI!'
CA JSON response with {"message": "Hello, FastAPI!"}
DA server error 500
Attempts:
2 left
💡 Hint
Look at the decorator and the return type of the function.
📝 Syntax
intermediate
1:30remaining
Which route decorator syntax is correct for a POST method?
You want to create a POST route at /submit. Which decorator syntax is correct?
A@app.post('/submit')
B@app.route('/submit', methods=['POST'])
C@app.get('/submit')
D@app.post_method('/submit')
Attempts:
2 left
💡 Hint
FastAPI uses HTTP method names as decorator functions.
🔧 Debug
advanced
2:30remaining
What error does this FastAPI route raise?
Examine this code snippet. What error will occur when starting the FastAPI app?
FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get('/items/{item_id}')
async def read_item(item_id: int):
    return {'item_id': item_id}

@app.get('/items/{item_id}')
async def read_item_duplicate(item_id: int):
    return {'item_id_duplicate': item_id}
ASyntaxError: Duplicate function names
BRuntimeError: Duplicate route path '/items/{item_id}'
CNo error, both routes work fine
DTypeError: Missing return statement
Attempts:
2 left
💡 Hint
Check if two routes have the same path and method.
state_output
advanced
2:00remaining
What is the response when using path and query parameters?
Given this FastAPI route, what is the JSON response for a GET request to /users/42?active=true?
FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get('/users/{user_id}')
async def get_user(user_id: int, active: bool = False):
    return {'user_id': user_id, 'active': active}
A{"user_id": 42, "active": false}
B{"user_id": "42", "active": "true"}
C404 Not Found
D{"user_id": 42, "active": true}
Attempts:
2 left
💡 Hint
Query parameters are converted to their declared types.
🧠 Conceptual
expert
3:00remaining
Which option correctly explains FastAPI route decorator behavior with multiple decorators?
What happens if you decorate a FastAPI function with multiple HTTP method decorators like @app.get() and @app.post() on the same function?
FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get('/multi')
@app.post('/multi')
async def multi_method():
    return {'message': 'Works for GET and POST'}
AThe route responds to both GET and POST requests at '/multi'.
BOnly the last decorator (@app.post) is effective; GET requests return 404.
CFastAPI raises a runtime error about multiple decorators on one function.
DThe route responds only to GET requests; POST requests return 405 Method Not Allowed.
Attempts:
2 left
💡 Hint
Think about how decorators stack in Python and FastAPI's routing.