0
0
FastAPIframework~20 mins

Accepting connections in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI Connection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a FastAPI app receives a GET request?
Consider a FastAPI app with this route:
from fastapi import FastAPI
app = FastAPI()

@app.get("/hello")
async def say_hello():
    return {"message": "Hello!"}

What is the output when a client sends a GET request to /hello?
FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/hello")
async def say_hello():
    return {"message": "Hello!"}
AThe server responds with plain text: Hello!
BThe server responds with JSON: {"message": "Hello!"}
CThe server returns a 404 Not Found error
DThe server crashes with a runtime error
Attempts:
2 left
💡 Hint
Think about what the return value of the route function means in FastAPI.
📝 Syntax
intermediate
2:00remaining
Which code correctly starts a FastAPI server on port 8080?
You want to run your FastAPI app on port 8080 using uvicorn. Which code snippet below does this correctly?
Auvicorn.run(app, port=8080, host="localhost")
Buvicorn.run(app, "0.0.0.0", 8080)
Cuvicorn.run(app, host="0.0.0.0", port=8080)
Duvicorn.run(app, port="8080", host=0.0.0.0)
Attempts:
2 left
💡 Hint
Check the parameter names and types uvicorn.run expects.
🔧 Debug
advanced
2:00remaining
Why does this FastAPI app fail to accept connections?
Given this code:
from fastapi import FastAPI
app = FastAPI()

@app.get("/")
def read_root():
    return {"msg": "Hi"}

You run uvicorn main:app but get no response on http://localhost:8000/. What is the likely cause?
FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/")
def read_root():
    return {"msg": "Hi"}
AThe app variable is not named correctly for uvicorn to find
BThe route function is not async, so uvicorn ignores it
CThe server is not running because uvicorn was not installed
DThe server is running but the route returns a 404 because of a typo
Attempts:
2 left
💡 Hint
Check the command and the app variable name in the code.
state_output
advanced
2:00remaining
What is the output after multiple requests to this FastAPI app?
Consider this FastAPI app:
from fastapi import FastAPI
app = FastAPI()
counter = 0

@app.get("/count")
async def count():
    global counter
    counter += 1
    return {"count": counter}

If a client sends 3 GET requests to /count in a row, what is the JSON response to the third request?
FastAPI
from fastapi import FastAPI
app = FastAPI()
counter = 0

@app.get("/count")
async def count():
    global counter
    counter += 1
    return {"count": counter}
AThe server crashes with a runtime error
B{"count": 1}
C{"count": 0}
D{"count": 3}
Attempts:
2 left
💡 Hint
Think about how the global variable changes with each request.
🧠 Conceptual
expert
2:00remaining
Which statement about FastAPI connection handling is true?
FastAPI uses ASGI servers like uvicorn to accept connections. Which of these statements is correct?
AFastAPI can handle many connections concurrently using async functions and event loop
BFastAPI requires all route functions to be synchronous to accept connections
CFastAPI automatically retries failed connections without developer code
DFastAPI handles connections synchronously, blocking other requests until one finishes
Attempts:
2 left
💡 Hint
Think about how async functions and ASGI servers work together.