0
0
FastAPIframework~20 mins

Serving static files in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Static Files Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when accessing a static file URL?
Given this FastAPI app code, what will be the HTTP response status code when accessing /static/example.txt if the file exists in the static folder?
FastAPI
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles

app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
A500 Internal Server Error
B200 OK with the contents of example.txt
C302 Redirect to /static/
D404 Not Found error
Attempts:
2 left
💡 Hint
Think about what happens when a static file exists and is served by StaticFiles.
📝 Syntax
intermediate
2:00remaining
Which option correctly mounts a static directory in FastAPI?
Select the code snippet that correctly mounts the 'assets' folder at the URL path '/assets' using FastAPI.
Aapp.mount("/assets", StaticFiles(directory="assets"))
Bapp.mount("assets", StaticFiles(directory="/assets"), name="assets")
Capp.mount("/assets", StaticFiles(directory="assets"), name="assets")
Dapp.mount("/assets", StaticFiles(path="assets"), name="assets")
Attempts:
2 left
💡 Hint
Check the parameter names and URL path format.
🔧 Debug
advanced
2:00remaining
Why does this FastAPI app return 404 for static files?
Consider this code snippet: from fastapi import FastAPI from fastapi.staticfiles import StaticFiles app = FastAPI() app.mount("/static", StaticFiles(directory="static_files"), name="static") The folder 'static_files' exists and contains 'image.png'. Accessing '/static/image.png' returns 404. What is the most likely cause?
AStaticFiles requires an absolute path, not a relative one.
BThe file 'image.png' is not readable by the server.
CThe app must include a route to serve static files explicitly.
DThe folder name 'static_files' is incorrect or misspelled.
Attempts:
2 left
💡 Hint
Check the folder name and path carefully.
state_output
advanced
2:00remaining
What happens if you mount two static directories on the same path?
Given this FastAPI code: app.mount("/static", StaticFiles(directory="static1"), name="static1") app.mount("/static", StaticFiles(directory="static2"), name="static2") What will be the behavior when accessing '/static/file.txt'?
AThe second mount overrides the first; files served from 'static2'.
BFiles are served from both directories merged together.
CFastAPI raises an error on app startup due to duplicate mounts.
DFiles are served from 'static1' only; second mount ignored.
Attempts:
2 left
💡 Hint
Think about how mounting the same path twice works.
🧠 Conceptual
expert
2:00remaining
Which statement about FastAPI StaticFiles is true?
Choose the correct statement about serving static files with FastAPI's StaticFiles.
AStaticFiles automatically sets correct MIME types based on file extensions.
BStaticFiles requires manual setting of Content-Type headers for each file.
CStaticFiles can only serve files from the root directory of the project.
DStaticFiles disables caching by default to always serve fresh files.
Attempts:
2 left
💡 Hint
Think about how web servers handle file types.