0
0
FastAPIframework~20 mins

Async path operations in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async FastAPI 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 async FastAPI endpoint?
Consider this FastAPI async endpoint. What will the client receive when calling /hello?
FastAPI
from fastapi import FastAPI
import asyncio

app = FastAPI()

@app.get('/hello')
async def say_hello():
    await asyncio.sleep(0.1)
    return {'message': 'Hello, async world!'}
A{"message": "Hello, async world!"}
BSyntaxError due to missing await
C{"message": "Hello, world!"}
DRuntimeError because asyncio.sleep is blocking
Attempts:
2 left
💡 Hint
Remember async functions can use await to pause without blocking.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines an async FastAPI path operation?
Select the option that correctly defines an async GET endpoint in FastAPI that returns a JSON with key 'status' and value 'ok'.
A
from fastapi import FastAPI
app = FastAPI()

@app.get('/status')
async def status():
    return {'status': 'ok'}
B
from fastapi import FastAPI
app = FastAPI()

@app.get('/status')
def status():
    await return {'status': 'ok'}
C
from fastapi import FastAPI
app = FastAPI()

@app.get('/status')
async def status():
    return await {'status': 'ok'}
D
from fastapi import FastAPI
app = FastAPI()

@app.get('/status')
async def status():
    await {'status': 'ok'}
Attempts:
2 left
💡 Hint
Only async functions can use await, and await must be used with awaitable objects.
state_output
advanced
2:00remaining
What is the output when calling this async endpoint twice quickly?
Given this FastAPI async endpoint, what will be the output of two quick calls to /counter?
FastAPI
from fastapi import FastAPI
import asyncio

app = FastAPI()
counter = 0

@app.get('/counter')
async def get_counter():
    global counter
    await asyncio.sleep(0.05)
    counter += 1
    return {'count': counter}
A[{"count": 1}, {"count": 1}]
B[{"count": 1}, {"count": 2}]
C[{"count": 2}, {"count": 3}]
DRuntimeError due to race condition
Attempts:
2 left
💡 Hint
Think about how async functions run and how global variables update.
🔧 Debug
advanced
2:00remaining
Which option causes a runtime error in this async FastAPI endpoint?
Identify which option will cause a runtime error when the endpoint /data is called.
FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get('/data')
async def get_data():
    data = {'a': 1, 'b': 2}
    result = await data.get('a')
    return {'result': result}
ASyntaxError due to missing colon
BReturns {'result': 1}
CTypeError: object int can't be used in 'await' expression
DKeyError because 'a' is missing
Attempts:
2 left
💡 Hint
Remember await only works with awaitable objects like coroutines.
🧠 Conceptual
expert
2:00remaining
Why should FastAPI path operations be async when performing I/O?
Select the best explanation why defining FastAPI path operations as async functions is important when they perform I/O operations like database queries or HTTP calls.
AAsync path operations automatically cache I/O results to speed up responses.
BAsync path operations prevent any exceptions from occurring during I/O.
CAsync path operations convert blocking I/O calls into CPU-bound tasks to speed up processing.
DAsync path operations allow FastAPI to handle other requests while waiting for I/O, improving concurrency and throughput.
Attempts:
2 left
💡 Hint
Think about what happens when a server waits for slow tasks.