Bird
Raised Fist0
FastAPIframework~20 mins

Path operations (GET, POST, PUT, DELETE) in FastAPI - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
FastAPI Path Operations 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 GET path operation?
Consider this FastAPI code snippet. What will be the response when a GET request is made to /items/42?
FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get('/items/{item_id}')
async def read_item(item_id: int):
    return {"item_id": item_id, "message": f"Item {item_id} found"}
A404 Not Found
B{"item_id": 42, "message": "Item 42 found"}
C{"item_id": 42}
D{"item_id": "42", "message": "Item 42 found"}
Attempts:
2 left
💡 Hint
Remember that FastAPI converts path parameters to the declared type.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a POST path operation to create an item?
You want to create a POST path operation in FastAPI that accepts an item_id as a path parameter and returns a confirmation message. Which code snippet is correct?
A
from fastapi import FastAPI
app = FastAPI()

@app.post('/items/{item_id}')
async def create_item(item_id: int):
    return {"message": f"Item {item_id} created"}
B
from fastapi import FastAPI
app = FastAPI()

@app.get('/items/{item_id}')
async def create_item(item_id: int):
    return {"message": f"Item {item_id} created"}
C
from fastapi import FastAPI
app = FastAPI()

@app.post('/items')
async def create_item(item_id: int):
    return {"message": f"Item {item_id} created"}
D
from fastapi import FastAPI
app = FastAPI()

@app.post('/items/{item_id}')
def create_item(item_id: int):
    return {"message": f"Item {item_id} created"}
Attempts:
2 left
💡 Hint
Check the HTTP method decorator and parameter types.
❓ state_output
advanced
2:00remaining
What is the value of the item after this PUT request?
Given this FastAPI app, what will be the value of items[3] after a PUT request to /items/3 with JSON body {"name": "NewName"}?
FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

items = {3: {"name": "OldName"}}

class Item(BaseModel):
    name: str

@app.put('/items/{item_id}')
async def update_item(item_id: int, item: Item):
    items[item_id] = item.dict()
    return items[item_id]
A{"name": "OldName"}
B{"name": null}
C{"name": "NewName"}
DKeyError
Attempts:
2 left
💡 Hint
The PUT operation replaces the item with the new data.
🔧 Debug
advanced
2:00remaining
Which option causes a runtime error when deleting an item?
Given this FastAPI app, which DELETE path operation code will cause a runtime error if the item does not exist?
FastAPI
from fastapi import FastAPI
app = FastAPI()
items = {1: "a", 2: "b"}
A
@app.delete('/items/{item_id}')
async def delete_item(item_id: int):
    try:
        del items[item_id]
    except KeyError:
        return {"message": "Not found"}
    return {"message": "Deleted"}
B
@app.delete('/items/{item_id}')
async def delete_item(item_id: int):
    items.pop(item_id, None)
    return {"message": "Deleted"}
C
@app.delete('/items/{item_id}')
async def delete_item(item_id: int):
    if item_id in items:
        del items[item_id]
    return {"message": "Deleted"}
D
@app.delete('/items/{item_id}')
async def delete_item(item_id: int):
    del items[item_id]
    return {"message": "Deleted"}
Attempts:
2 left
💡 Hint
Deleting a non-existent key with del causes an error.
🧠 Conceptual
expert
2:00remaining
Which HTTP method is idempotent and safe to call multiple times without changing the resource state?
In RESTful APIs using FastAPI, which HTTP method among GET, POST, PUT, DELETE is idempotent and safe to call multiple times without side effects?
AGET
BPOST
CPUT
DDELETE
Attempts:
2 left
💡 Hint
Idempotent means the same request can be repeated without changing the result.

Practice

(1/5)
1. Which HTTP method in FastAPI is typically used to retrieve data without changing anything on the server?
easy
A. DELETE
B. POST
C. PUT
D. GET

Solution

  1. Step 1: Understand HTTP methods purpose

    GET is used to read or retrieve data without modifying it.
  2. Step 2: Match method to FastAPI operation

    FastAPI uses @app.get() decorator for GET requests to fetch data.
  3. Final Answer:

    GET -> Option D
  4. Quick Check:

    GET = Read data [OK]
Hint: GET always fetches data without side effects [OK]
Common Mistakes:
  • Confusing GET with POST which creates data
  • Using PUT for reading data
  • Thinking DELETE retrieves data
2. Which of the following is the correct syntax to define a POST path operation in FastAPI to create a new item?
easy
A. @app.post('/items/')\ndef create_item(): pass
B. @app.get('/items/')\ndef create_item(): pass
C. @app.put('/items/')\ndef create_item(): pass
D. @app.delete('/items/')\ndef create_item(): pass

Solution

  1. Step 1: Identify the HTTP method for creating data

    POST is used to create new resources in REST APIs.
  2. Step 2: Match FastAPI decorator for POST

    FastAPI uses @app.post() decorator to handle POST requests.
  3. Final Answer:

    @app.post('/items/')\ndef create_item(): pass -> Option A
  4. Quick Check:

    POST = Create data [OK]
Hint: Use @app.post() for creating new data [OK]
Common Mistakes:
  • Using @app.get() for creating data
  • Confusing PUT with POST
  • Using DELETE for creation
3. What will be the response when this FastAPI code is called with a PUT request to update item with id 5?
from fastapi import FastAPI
app = FastAPI()

items = {5: "old value"}

@app.put('/items/{item_id}')
async def update_item(item_id: int):
    items[item_id] = "new value"
    return items[item_id]
medium
A. "new value"
B. "old value"
C. KeyError
D. None

Solution

  1. Step 1: Understand the PUT operation code

    The function updates the dictionary value for the given item_id to "new value".
  2. Step 2: Check the returned value

    The function returns the updated value, which is "new value".
  3. Final Answer:

    "new value" -> Option A
  4. Quick Check:

    PUT updates and returns new value [OK]
Hint: PUT replaces existing data and returns updated value [OK]
Common Mistakes:
  • Expecting old value after update
  • Thinking KeyError occurs without check
  • Assuming None is returned
4. Identify the error in this FastAPI DELETE operation code:
from fastapi import FastAPI
app = FastAPI()

items = {1: "apple", 2: "banana"}

@app.delete('/items/{item_id}')
async def delete_item(item_id: int):
    del items[item_id]
    return {"message": "Item deleted"}
medium
A. Missing return statement
B. Wrong HTTP method decorator
C. Deleting item without checking if it exists
D. Function should not be async

Solution

  1. Step 1: Analyze the delete operation

    The code deletes an item by key without verifying if the key exists in the dictionary.
  2. Step 2: Understand possible runtime error

    If the item_id does not exist, del will raise a KeyError causing a server error.
  3. Final Answer:

    Deleting item without checking if it exists -> Option C
  4. Quick Check:

    Delete needs existence check [OK]
Hint: Always check if item exists before deleting [OK]
Common Mistakes:
  • Assuming item always exists
  • Ignoring KeyError on delete
  • Thinking async is wrong here
5. You want to create a FastAPI app that supports all four main HTTP methods (GET, POST, PUT, DELETE) for managing a list of users stored in a dictionary. Which approach correctly handles updating a user only if it exists, creating a new user, retrieving a user by ID, and deleting a user safely?
hard
A. Use @app.get to retrieve, @app.post to create, @app.put to update without checking existence, and @app.delete to delete without checking existence
B. Use @app.get to retrieve, @app.post to create, @app.put to update with existence check, and @app.delete to delete with existence check
C. Use only @app.post for all operations to simplify code
D. Use @app.put to create and update, @app.get to delete, @app.post to retrieve

Solution

  1. Step 1: Match HTTP methods to CRUD operations

    GET retrieves, POST creates, PUT updates, DELETE removes resources.
  2. Step 2: Ensure safe update and delete

    Update and delete should check if the user exists to avoid errors.
  3. Final Answer:

    Use @app.get to retrieve, @app.post to create, @app.put to update with existence check, and @app.delete to delete with existence check -> Option B
  4. Quick Check:

    Correct HTTP methods with safety checks [OK]
Hint: Match HTTP verbs to actions and check existence before update/delete [OK]
Common Mistakes:
  • Skipping existence checks causing errors
  • Using wrong HTTP methods for actions
  • Trying to do all actions with one method
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard