Bird
Raised Fist0
FastAPIframework~20 mins

Accepting connections in FastAPI - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does it mean when a FastAPI app is "accepting connections"?
easy
A. The app is listening for incoming network requests on a specific address and port.
B. The app is sending data to a database.
C. The app is shutting down all network activity.
D. The app is compiling Python code.

Solution

  1. Step 1: Understand the term "accepting connections" in networking

    Accepting connections means the app waits and listens for requests from users or other programs on a network address and port.
  2. Step 2: Relate to FastAPI app behavior

    FastAPI apps accept connections by running a server that listens on a host and port for incoming HTTP requests.
  3. Final Answer:

    The app is listening for incoming network requests on a specific address and port. -> Option A
  4. Quick Check:

    Accepting connections = listening on address and port [OK]
Hint: Accepting connections means listening for requests [OK]
Common Mistakes:
  • Confusing accepting connections with sending data
  • Thinking it means shutting down the app
  • Mixing it with code compilation
2. Which of the following is the correct way to start a FastAPI app to accept connections on all network interfaces at port 8000?
easy
A. uvicorn.run(app, host="127.0.0.1", port=8080)
B. uvicorn.run(app, host="0.0.0.0", port=8000)
C. uvicorn.run(app, host="localhost", port=80)
D. uvicorn.run(app, host="192.168.1.1", port=5000)

Solution

  1. Step 1: Identify the host for all network interfaces

    The host "0.0.0.0" means the app listens on all network interfaces, allowing external access.
  2. Step 2: Check the port number

    Port 8000 is the default port commonly used for FastAPI apps.
  3. Final Answer:

    uvicorn.run(app, host="0.0.0.0", port=8000) -> Option B
  4. Quick Check:

    Host 0.0.0.0 + port 8000 = correct start command [OK]
Hint: Use host 0.0.0.0 for all interfaces [OK]
Common Mistakes:
  • Using localhost instead of 0.0.0.0 for external access
  • Choosing wrong port numbers
  • Confusing IP addresses with hostnames
3. Given this code snippet, what will happen when you run it?
import uvicorn
from fastapi import FastAPI
app = FastAPI()

@app.get("/")
async def read_root():
    return {"message": "Hello World"}

uvicorn.run(app, host="127.0.0.1", port=9000)
medium
A. The app will listen on all interfaces at port 9000 but return an error on root URL.
B. The app will fail to start due to syntax error.
C. The app will listen on localhost at port 9000 and respond with JSON {"message": "Hello World"} on root URL.
D. The app will listen on localhost but on default port 8000.

Solution

  1. Step 1: Analyze the uvicorn.run parameters

    The app runs on host "127.0.0.1" (localhost) and port 9000, so it listens only on local machine at port 9000.
  2. Step 2: Check the route and response

    The root path "/" returns JSON {"message": "Hello World"} asynchronously, so a GET request to localhost:9000/ returns this JSON.
  3. Final Answer:

    The app will listen on localhost at port 9000 and respond with JSON {"message": "Hello World"} on root URL. -> Option C
  4. Quick Check:

    Host 127.0.0.1 + port 9000 + root JSON response = B [OK]
Hint: Check host, port, and route response carefully [OK]
Common Mistakes:
  • Assuming app listens on all interfaces
  • Expecting default port 8000 instead of 9000
  • Thinking code has syntax errors
4. You wrote this code to start your FastAPI app but it crashes with an error:
import uvicorn
from fastapi import FastAPI
app = FastAPI()

uvicorn.run(app, host="localhost", port="8000")

What is the likely cause of the error?
medium
A. Missing async keyword before uvicorn.run call.
B. The host "localhost" is invalid and causes the crash.
C. FastAPI app must be run with app.run(), not uvicorn.run().
D. The port number should be an integer, not a string.

Solution

  1. Step 1: Check the port argument type

    The port argument must be an integer, but here it is passed as a string "8000" which causes a type error.
  2. Step 2: Verify host and method usage

    Host "localhost" is valid, and uvicorn.run is the correct method to start FastAPI apps.
  3. Final Answer:

    The port number should be an integer, not a string. -> Option D
  4. Quick Check:

    Port must be int, not str [OK]
Hint: Port must be a number, not text [OK]
Common Mistakes:
  • Passing port as string instead of int
  • Thinking host "localhost" is invalid
  • Using wrong method to start app
5. You want your FastAPI app to accept connections only from your local machine and run on port 8080. Which code snippet correctly achieves this and why?
Option A:
uvicorn.run(app, host="0.0.0.0", port=8080)

Option B:
uvicorn.run(app, host="127.0.0.1", port=8080)

Option C:
uvicorn.run(app, host="localhost", port=80)

Option D:
uvicorn.run(app, host="192.168.1.100", port=8080)
hard
A. Option B, because 127.0.0.1 restricts connections to local machine only on port 8080.
B. Option A, because 0.0.0.0 listens on all interfaces including local machine.
C. Option C, because localhost always means local machine and port 80 is standard.
D. Option D, because 192.168.1.100 is the local IP and port 8080 is correct.

Solution

  1. Step 1: Understand host IP meanings

    Host "127.0.0.1" means the app listens only on the local machine (loopback interface), blocking external connections.
  2. Step 2: Check port number

    Port 8080 is specified correctly and is a valid port for local testing.
  3. Step 3: Evaluate other options

    0.0.0.0 listens on all interfaces, allowing external access; localhost on port 80 uses the wrong port; 192.168.1.100 binds to a specific LAN IP which may not restrict to local machine only.
  4. Final Answer:

    Option A, because 127.0.0.1 restricts connections to local machine only on port 8080. -> Option A
  5. Quick Check:

    Local only = 127.0.0.1 + port 8080 [OK]
Hint: Use 127.0.0.1 for local-only access [OK]
Common Mistakes:
  • Choosing 0.0.0.0 for local-only access
  • Confusing localhost and 127.0.0.1
  • Using wrong port number
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