0
0
Flaskframework~20 mins

Service layer pattern in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Service Layer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Flask service layer call?

Consider a Flask app using a service layer to fetch user data. What will be printed when calling print(user_service.get_user_name(2))?

Flask
class UserService:
    def __init__(self):
        self.users = {1: 'Alice', 2: 'Bob', 3: 'Charlie'}

    def get_user_name(self, user_id):
        return self.users.get(user_id, 'Unknown')

user_service = UserService()
print(user_service.get_user_name(2))
AAlice
BBob
CUnknown
DCharlie
Attempts:
2 left
💡 Hint

Look at the dictionary key used in the get_user_name call.

state_output
intermediate
2:00remaining
What is the state of the service after adding a new user?

After running user_service.add_user(4, 'Diana'), what will user_service.users contain?

Flask
class UserService:
    def __init__(self):
        self.users = {1: 'Alice', 2: 'Bob', 3: 'Charlie'}

    def add_user(self, user_id, name):
        self.users[user_id] = name

user_service = UserService()
user_service.add_user(4, 'Diana')
A{1: 'Alice', 2: 'Bob', 3: 'Charlie'}
B{}
C{4: 'Diana'}
D{1: 'Alice', 2: 'Bob', 3: 'Charlie', 4: 'Diana'}
Attempts:
2 left
💡 Hint

Adding a user updates the users dictionary.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a Flask service method to delete a user?

Choose the correct method to remove a user by user_id from the users dictionary in the service layer.

Flask
class UserService:
    def __init__(self):
        self.users = {1: 'Alice', 2: 'Bob', 3: 'Charlie'}

    def delete_user(self, user_id):
        # method to remove user
        pass
A
def delete_user(self, user_id):
    self.users.pop(user_id, None)
B
def delete_user(self, user_id):
    self.users.remove(user_id)
C
def delete_user(self, user_id):
    del self.users[user_id]
D
def delete_user(self, user_id):
    self.users.delete(user_id)
Attempts:
2 left
💡 Hint

Use a dictionary method that safely removes a key without error if missing.

🔧 Debug
advanced
2:00remaining
Why does this Flask service method raise a KeyError?

Given this service method, why does calling get_user_name(5) raise a KeyError?

Flask
class UserService:
    def __init__(self):
        self.users = {1: 'Alice', 2: 'Bob', 3: 'Charlie'}

    def get_user_name(self, user_id):
        return self.users[user_id]

user_service = UserService()
user_service.get_user_name(5)
ABecause the method should use self.users.get(user_id) instead of self.users[user_id]
BBecause the users dictionary is empty
CBecause user_id 5 does not exist in the users dictionary and direct access raises KeyError
DBecause the UserService class is missing an __init__ method
Attempts:
2 left
💡 Hint

Accessing a missing key in a dictionary directly causes an error.

🧠 Conceptual
expert
2:00remaining
What is the main benefit of using a service layer in a Flask app?

Choose the best explanation for why a service layer is used in Flask applications.

AIt separates business logic from route handlers, making code easier to maintain and test
BIt automatically generates HTML templates for the app
CIt replaces the need for a database by storing data in memory
DIt handles user authentication and authorization automatically
Attempts:
2 left
💡 Hint

Think about code organization and responsibilities.