Depends — Dependency Injection Basics — Concept Check
Questions on Depends — Dependency Injection Basics. Depends(fn) tells FastAPI: run fn first (resolving ITS parameters the same way) and hand me the result. Shared logic — pagination, auth, DB sessions — becomes declared-once, injected-everywhere.
Questions in this quiz
7 questions on Depends — Dependency Injection Basics — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A FastAPI application has multiple endpoints requiring pagination. How can you ensure consistent pagination logic across all endpoints?
- AUse Depends to inject a shared pagination function into each endpoint
- BUse a global variable to store pagination settings
- CDefine pagination logic in the main function
- DManually copy the pagination logic into each endpoint
2.In FastAPI, what happens if a dependency is used multiple times within a single request?
- AThe dependency is ignored after the first execution
- BThe dependency is executed once and its result is cached
- CThe dependency results are merged
- DThe dependency is executed multiple times
3.An endpoint requires both the current user and their college information. How does FastAPI handle this?
- AIt requires manual management of dependency execution
- BIt executes each dependency separately without caching
- CIt resolves the dependency tree and caches results, avoiding duplicate calls
- DIt fails if dependencies are used more than once
4.Why might a developer choose to use Depends in FastAPI?
- ATo increase the complexity of the codebase
- BTo manually manage all dependency execution
- CTo avoid using any shared logic
- DTo simplify code by reusing shared logic across endpoints
5.A developer notices that a dependency is not being executed as expected. What could be a reason for this behavior?
- AThe dependency is incorrectly defined
- BThe dependency is executed in a different thread
- CFastAPI does not support dependencies
- DThe dependency result is cached from a previous execution
6.How does FastAPI document dependencies in its generated documentation?
- ADependencies are automatically included in the endpoint's documentation
- BOnly top-level dependencies are documented
- CDevelopers must manually document each dependency
- DDependencies are not documented
7.What happens if a dependency in FastAPI raises an HTTPException?
- AThe exception is logged but does not affect the request
- BThe exception is ignored and the request continues
- CThe exception results in a 500 Internal Server Error
- DThe exception is propagated and the request fails with the specified status code