0
0
FastAPIframework~20 mins

CRUD operations in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI CRUD 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 GET endpoint?

Consider this FastAPI endpoint that returns a user by ID from a dictionary.

from fastapi import FastAPI, HTTPException
app = FastAPI()
users = {1: "Alice", 2: "Bob"}

@app.get("/users/{user_id}")
async def read_user(user_id: int):
    if user_id in users:
        return {"user": users[user_id]}
    raise HTTPException(status_code=404, detail="User not found")

What will be the JSON response when a client requests /users/2?

FastAPI
from fastapi import FastAPI, HTTPException
app = FastAPI()
users = {1: "Alice", 2: "Bob"}

@app.get("/users/{user_id}")
async def read_user(user_id: int):
    if user_id in users:
        return {"user": users[user_id]}
    raise HTTPException(status_code=404, detail="User not found")
A{"user": "Bob"}
B{"user": "Alice"}
C{"detail": "User not found"}
D404 Not Found error with empty body
Attempts:
2 left
💡 Hint

Check the dictionary key used to fetch the user.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a FastAPI POST endpoint to create a new item?

You want to create a POST endpoint in FastAPI that accepts a JSON body with a name field and returns the created item with an id.

Which code snippet is syntactically correct and follows FastAPI patterns?

A
from fastapi import FastAPI
app = FastAPI()

@app.post("/items")
async def create_item(name: str):
    return {"id": 1, "name": name}
B
from fastapi import FastAPI, Body
app = FastAPI()

@app.post("/items")
async def create_item(name: str = Body(...)):
    return {"id": 1, "name": name}
C
from fastapi import FastAPI
app = FastAPI()

@app.post("/items")
def create_item(name: str):
    return {"id": 1, "name": name}
D
from fastapi import FastAPI
app = FastAPI()

@app.post("/items")
async def create_item(name):
    return {"id": 1, "name": name}
Attempts:
2 left
💡 Hint

Think about how FastAPI extracts data from the request body.

🔧 Debug
advanced
2:00remaining
Why does this FastAPI PUT endpoint raise a 422 Unprocessable Entity error?

Examine this FastAPI PUT endpoint meant to update a user's name:

from fastapi import FastAPI
app = FastAPI()
users = {1: "Alice", 2: "Bob"}

@app.put("/users/{user_id}")
async def update_user(user_id: int, name: str):
    if user_id in users:
        users[user_id] = name
        return {"user": users[user_id]}
    return {"error": "User not found"}

When a client sends a PUT request with JSON body {"name": "Charlie"}, the server responds with 422 error. Why?

FastAPI
from fastapi import FastAPI
app = FastAPI()
users = {1: "Alice", 2: "Bob"}

@app.put("/users/{user_id}")
async def update_user(user_id: int, name: str):
    if user_id in users:
        users[user_id] = name
        return {"user": users[user_id]}
    return {"error": "User not found"}
AThe users dictionary is empty, so update fails
BThe 'user_id' parameter is missing in the function signature
CThe endpoint is missing a return statement
DFastAPI expects 'name' as a query parameter, but client sent it in JSON body
Attempts:
2 left
💡 Hint

Check how FastAPI matches function parameters to request data.

state_output
advanced
2:00remaining
What is the state of the users dictionary after these FastAPI DELETE requests?

Given this FastAPI app:

from fastapi import FastAPI, HTTPException
app = FastAPI()
users = {1: "Alice", 2: "Bob", 3: "Charlie"}

@app.delete("/users/{user_id}")
async def delete_user(user_id: int):
    if user_id in users:
        del users[user_id]
        return {"message": "Deleted"}
    raise HTTPException(status_code=404, detail="User not found")

If a client sends DELETE requests to /users/2 then /users/3, what is the content of users afterwards?

FastAPI
from fastapi import FastAPI, HTTPException
app = FastAPI()
users = {1: "Alice", 2: "Bob", 3: "Charlie"}

@app.delete("/users/{user_id}")
async def delete_user(user_id: int):
    if user_id in users:
        del users[user_id]
        return {"message": "Deleted"}
    raise HTTPException(status_code=404, detail="User not found")
A{1: "Alice"}
B{2: "Bob", 3: "Charlie"}
C{}
D{1: "Alice", 2: "Bob", 3: "Charlie"}
Attempts:
2 left
💡 Hint

Think about what happens when you delete keys from a dictionary.

🧠 Conceptual
expert
2:00remaining
Which statement best describes idempotency in FastAPI CRUD operations?

Idempotency means that making the same request multiple times has the same effect as making it once.

Which FastAPI CRUD method is idempotent by design?

ADELETE endpoint that removes a resource and returns 404 if called again
BPATCH endpoint partially updating a resource with different data each time
CGET endpoint fetching a resource without changing state
DPOST endpoint creating a new resource each time it is called
Attempts:
2 left
💡 Hint

Think about which HTTP methods do not change server state.