Dependencies with yield & Classes — Setup and Teardown — Concept Check
Questions on Dependencies with yield & Classes — Setup and Teardown. A yield dependency runs setup, hands over the resource, then runs cleanup after the response — the DB-session pattern. Class dependencies carry configuration; both compose with everything else.
Questions in this quiz
8 questions on Dependencies with yield & Classes — Setup and Teardown — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A FastAPI application uses a yield dependency for database sessions. What happens if an exception is raised during the endpoint execution?
- AThe session remains open and uncommitted.
- BThe session is closed without rollback.
- CThe session is rolled back and closed.
- DThe session is committed and closed.
2.In a FastAPI app, a RateLimiter class is used with a limit of 3 requests per 5 minutes. What happens if a client makes a 4th request within 5 minutes?
- AThe request is processed normally.
- BThe request is queued for later processing.
- CThe request is silently dropped.
- DA 429 HTTPException is raised.
3.A developer uses a class dependency for feature flags in a FastAPI app. What is the main advantage of this approach?
- AIt guarantees resource cleanup after each request.
- BIt allows configuration per request.
- CIt simplifies the code by removing all configurations.
- DIt automatically scales the application.
4.What is the purpose of the finally block in a yield dependency pattern?
- ATo commit changes only if the endpoint succeeds.
- BTo ensure resource cleanup regardless of success or failure.
- CTo handle exceptions that occur during the endpoint execution.
- DTo configure dependencies dynamically.
5.A developer mistakenly believes that using yield in dependencies automatically commits changes. What is the correct behavior?
- AChanges are committed only if no exceptions occur after yield.
- BChanges are always committed after yield.
- CChanges are never committed automatically; manual commit is required.
- DYield automatically handles both commit and rollback.
6.Which of the following scenarios best illustrates the use of a class dependency in FastAPI?
- AAutomatically scaling the application based on load.
- BEnsuring database sessions are closed after each request.
- CHandling exceptions during request processing.
- DConfiguring different rate limits for different endpoints.
7.In a FastAPI application, what happens if an HTTPException is raised after the yield statement in a dependency?
- AThe exception is ignored, and the request continues.
- BThe request is terminated without cleanup.
- CThe exception is caught and handled silently.
- DThe exception is raised, and the finally block ensures cleanup.
8.A developer thinks that class dependencies in FastAPI can only be used for rate limiting. What is the broader applicability of class dependencies?
- AThey can be used for any configurable dependency, like feature flags.
- BThey are primarily for managing database connections.
- CThey are limited to rate limiting and cannot be used elsewhere.
- DThey are only used for handling HTTP exceptions.