What will be the output if the event is {'message': 'Hi'}?
medium
A. {'statusCode': 404, 'body': 'Hi'}
B. Error: KeyError 'message'
C. {'statusCode': 200, 'body': 'Hi'}
D. None
Solution
Step 1: Analyze the return statement
The function returns a dictionary with statusCode set to 200 and body set to the value of event['message'].
Step 2: Substitute event value
Given event = {'message': 'Hi'}, event['message'] is 'Hi'. So the return is {'statusCode': 200, 'body': 'Hi'}.
Final Answer:
{'statusCode': 200, 'body': 'Hi'} -> Option C
Quick Check:
Return body matches event message [OK]
Hint: Return uses event['message'] directly for body [OK]
Common Mistakes:
Assuming wrong statusCode
Expecting error when key exists
Returning None by mistake
4. What is wrong with this AWS Lambda handler function in Python?
def handler(event):
return 'Done'
medium
A. Missing the required context parameter
B. Return value must be a dictionary
C. Function name must be lambda_handler
D. No error, this is valid
Solution
Step 1: Check function parameters
AWS Lambda handler functions must accept two parameters: event and context. This function only has event.
Step 2: Validate other options
Return type can vary by trigger, so returning a string is allowed. Function name can be configured, so handler is acceptable. Hence, only missing context is an error.
Final Answer:
Missing the required context parameter -> Option A
Quick Check:
Lambda handler needs event and context parameters [OK]
Hint: Always include event and context parameters [OK]
Common Mistakes:
Omitting context parameter
Thinking return must be dict always
Assuming fixed function name
5. You want your AWS Lambda handler to process an event with optional user key. If user is missing, return status 400 with message 'User missing'. Otherwise, return status 200 with 'Hello, <user>'. Which handler code correctly implements this?
hard
A. def handler(event):
if 'user' in event:
return {'statusCode': 400, 'body': 'User missing'}
return {'statusCode': 200, 'body': f"Hello, {event['user']}"}
B. def handler(event, context):
return {'statusCode': 200, 'body': f"Hello, {event['user']}"}
C. def handler(event, context):
if event['user'] == None:
return {'statusCode': 400, 'body': 'User missing'}
return {'statusCode': 200, 'body': 'Hello, user'}
D. def handler(event, context):
if 'user' not in event:
return {'statusCode': 400, 'body': 'User missing'}
return {'statusCode': 200, 'body': f"Hello, {event['user']}"}
Solution
Step 1: Check parameter correctness
Lambda handler must have event and context parameters.
Step 2: Validate logic for user key
def handler(event, context):
if 'user' not in event:
return {'statusCode': 400, 'body': 'User missing'}
return {'statusCode': 200, 'body': f"Hello, {event['user']}"} correctly checks if 'user' key is missing and returns 400 with message. Then returns 200 with personalized greeting. def handler(event, context):
return {'statusCode': 200, 'body': f"Hello, {event['user']}"} does not check for missing user. def handler(event, context):
if event['user'] == None:
return {'statusCode': 400, 'body': 'User missing'}
return {'statusCode': 200, 'body': 'Hello, user'} incorrectly checks if event['user'] == None which causes error if key missing. def handler(event):
if 'user' in event:
return {'statusCode': 400, 'body': 'User missing'}
return {'statusCode': 200, 'body': f"Hello, {event['user']}"} reverses logic and misses context.
Final Answer:
def handler(event, context):
if 'user' not in event:
return {'statusCode': 400, 'body': 'User missing'}
return {'statusCode': 200, 'body': f"Hello, {event['user']}" } -> Option D
Quick Check:
Check key presence before access to avoid errors [OK]
Hint: Check key exists before access; include event and context [OK]
Common Mistakes:
Missing context parameter
Accessing missing keys without check
Returning wrong status codes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.