1. What is async database integration?
AdvancedAnswer: All of the above
Async database integration uses async SQLAlchemy, databases library, or motor (MongoDB). Enables non-blocking I/O for better concurrency and performance.
24 questions that come up in FastAPI technical interviews, each with the answer and an explanation of why it is right.
Test yourself — 90 question bankAnswer: All of the above
Async database integration uses async SQLAlchemy, databases library, or motor (MongoDB). Enables non-blocking I/O for better concurrency and performance.
Answer: All of the above
Dependency injection in FastAPI uses Depends() to declare dependencies. It enables code reuse, shared logic, database connections, authentication, etc.
Answer: A modern, fast web framework for building APIs with Python
FastAPI is a modern, fast (high-performance) web framework for building APIs with Python 3.7+ based on standard Python type hints.
Answer: All of the above
Test FastAPI with TestClient: creates client for testing without running server. Use pytest for test framework. Supports async tests with pytest-asyncio.
Answer: Type hints
FastAPI heavily relies on Python type hints for data validation, serialization, and automatic API documentation generation.
Answer: Create a function and use Depends()
Define a dependency as a function, then use it with Depends(): `def get_db(): ...; @app.get("/") def read(db = Depends(get_db)):`. FastAPI calls it automatically.
Answer: pip install fastapi
Install FastAPI using pip: `pip install fastapi`. You also need an ASGI server like uvicorn: `pip install uvicorn[standard]`.
Answer: Both b and c
Sub-dependencies are dependencies that themselves have dependencies. FastAPI resolves the entire dependency tree automatically, creating chains of dependencies.
Answer: All of the above
Override dependencies in tests using app.dependency_overrides dict. Replace real database with test database, mock external APIs, etc.
Answer: app = FastAPI()
Create a FastAPI instance with `app = FastAPI()`. This creates the main application object that you use to define routes and configurations.
Answer: All of the above
Use generator functions with yield for database sessions. Code before yield runs before request, after yield runs after (cleanup). Example: get_db() yields session.
Answer: Both b and c
Integrate GraphQL using Strawberry or Graphene libraries. Add GraphQL endpoint to FastAPI app. Combine REST and GraphQL in same application.
Answer: All of the above
Create custom exception handlers with @app.exception_handler(ExceptionClass). Return custom error responses, log errors, send notifications, etc.
Answer: @app.get()
Use `@app.get("/path")` to define a GET endpoint. FastAPI provides decorators for all HTTP methods: get, post, put, delete, patch, options, head.
Answer: Both b and c
OAuth2PasswordBearer is a security scheme that extracts the bearer token from Authorization header. Used as dependency to protect endpoints.
Answer: All of the above
Implement JWT using python-jose: create tokens with user info and expiry, sign them, return to client. Verify tokens in dependency functions to protect endpoints.
Answer: uvicorn main:app --reload
Run FastAPI with uvicorn: `uvicorn main:app --reload` where main is the filename and app is the FastAPI instance. --reload enables auto-reload during development.
Answer: All of the above
Implement rate limiting using slowapi (Flask-Limiter port) or custom middleware. Limit requests per IP/user per time window. Prevents abuse and DDoS.
Answer: All of the above
Implement pagination with skip/limit query parameters. Return paginated data with total count. Create reusable pagination dependency for consistency.
Answer: Both b and c
APIRouter creates modular, reusable route groups. Define routes in separate files/modules, then include them in main app with app.include_router().
Answer: Auto-generated interactive docs at /docs and /redoc
FastAPI automatically generates interactive API documentation. Access Swagger UI at /docs and ReDoc at /redoc. Based on OpenAPI standard.
Answer: Both b and c
StreamingResponse sends content in chunks without loading entire response in memory. Useful for large files, video streaming, or generated content.
Answer: All of the above
Create router with APIRouter(), define routes on it, then include: `app.include_router(router, prefix="/items", tags=["items"])`.
Answer: Using curly braces in path
Define path parameters using curly braces: `@app.get("/items/{item_id}")`. The parameter is automatically passed to the function and validated.
The full FastAPI bank has 90 questions across 3 difficulty levels — timed, shuffled, and scored.
Take the FastAPI quiz