Bird
0
0

Given this Python code snippet using boto3 to receive a message from an SQS queue, what will be printed if the queue is empty?

medium📝 Predict Output Q4 of 15
AWS - SNS and SQS
Given this Python code snippet using boto3 to receive a message from an SQS queue, what will be printed if the queue is empty? ```python import boto3 sqs = boto3.client('sqs') response = sqs.receive_message(QueueUrl='https://sqs.region.amazonaws.com/123456789012/MyQueue') print(response.get('Messages')) ```
AA string 'No messages'
BAn empty list []
CA KeyError exception
DNone
Step-by-Step Solution
Solution:
  1. Step 1: Understand boto3 receive_message behavior when no messages

    If no messages are available, the 'Messages' key is not present in the response dictionary.
  2. Step 2: Check what response.get('Messages') returns if key missing

    The get method returns None if the key is missing, so print outputs None.
  3. Final Answer:

    None is printed because 'Messages' key is absent when queue is empty. -> Option D
  4. Quick Check:

    Empty queue receive_message = None [OK]
Quick Trick: response.get returns None if key missing [OK]
Common Mistakes:
  • Expecting empty list instead of None
  • Assuming KeyError is raised
  • Thinking a string message is returned

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More AWS Quizzes