Which file should contain the function to fetch user data from the database?
medium
A. routes/user.py
B. models/user.py
C. services/user_service.py
D. app/__init__.py
Solution
Step 1: Understand folder responsibilities
routes handles API endpoints, models defines data structures, and services contains business logic like fetching data.
Step 2: Match function purpose to folder
Fetching user data is business logic, so it belongs in services/user_service.py.
Final Answer:
services/user_service.py -> Option C
Quick Check:
Data fetching logic goes in services [OK]
Hint: Business logic like fetching data belongs in services [OK]
Common Mistakes:
Putting fetching logic in routes
Adding functions to models instead of services
Using __init__.py for logic
4. You see this folder structure in a FastAPI project:
app/
routes.py
models.py
services.py
database.py
What is a likely problem with this structure as the project grows?
medium
A. Too many files in root, making it hard to maintain
B. Using .py files instead of folders is invalid in FastAPI
C. Missing a templates folder for HTML rendering
D. Database code should be inside routes.py
Solution
Step 1: Analyze flat file structure
Having all files directly under app/ is okay for small projects but becomes cluttered and hard to maintain as code grows.
Step 2: Understand folder benefits
Using folders like routes/, models/, and services/ helps organize code by purpose and improves maintainability.
Final Answer:
Too many files in root, making it hard to maintain -> Option A
Quick Check:
Flat structure hurts maintainability [OK]
Hint: Avoid many files in root; use folders for organization [OK]
Common Mistakes:
Thinking .py files can't be used
Confusing database code location
Assuming templates folder is mandatory
5. You want to organize a FastAPI project that has multiple features like users, products, and orders. Which folder structure pattern best supports scalability and maintainability?
hard
A. Mix routes and models in the same folder to reduce folder count
B. Create separate folders for each feature with their own routes, models, and services inside
C. Keep all code files in the root folder for easy access
D. Put all routes in one routes folder, all models in one models folder, and all services in one services folder
Solution
Step 1: Understand feature-based folder structure
Organizing by feature means each feature (users, products, orders) has its own folder containing routes, models, and services, improving modularity.
Step 2: Compare with other patterns
Grouping all routes or models together can become large and hard to maintain. Keeping all files in root or mixing unrelated code reduces clarity.
Final Answer:
Create separate folders for each feature with their own routes, models, and services inside -> Option B
Quick Check:
Feature-based folders improve scalability [OK]
Hint: Use feature folders with routes, models, services inside [OK]
Common Mistakes:
Using flat folders for all code
Mixing unrelated code in one folder
Ignoring modular design for growth
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.