Bird
Raised Fist0
RabbitMQdevops~10 mins

Default exchange behavior in RabbitMQ - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to publish a message to the default exchange in RabbitMQ.

RabbitMQ
channel.basic_publish(exchange=[1], routing_key='task_queue', body='Hello World!')
Drag options to blanks, or click blank then click option'
A'amq.topic'
B'default'
C'amq.direct'
D''
Attempts:
3 left
💡 Hint
Common Mistakes
Using a named exchange like 'default' instead of the empty string.
Confusing the default exchange with 'amq.direct' or 'amq.topic'.
2fill in blank
medium

Complete the code to declare a queue that will receive messages from the default exchange.

RabbitMQ
channel.queue_declare(queue=[1], durable=True)
Drag options to blanks, or click blank then click option'
A'amq.direct'
B'my_queue'
C''
D'default'
Attempts:
3 left
💡 Hint
Common Mistakes
Using an empty string as queue name, which is invalid for queue declaration.
Confusing exchange names with queue names.
3fill in blank
hard

Fix the error in the code to correctly bind a queue to the default exchange.

RabbitMQ
channel.queue_bind(queue='my_queue', exchange=[1], routing_key='my_queue')
Drag options to blanks, or click blank then click option'
A''
B'amq.topic'
C'amq.direct'
D'default'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to bind a queue to the default exchange which is not allowed.
Using a named exchange instead of the empty string for the default exchange.
4fill in blank
hard

Fill both blanks to create a message consumer that listens to the queue bound to the default exchange.

RabbitMQ
channel.basic_consume(queue=[1], on_message_callback=[2], auto_ack=True)
Drag options to blanks, or click blank then click option'
A'my_queue'
Bcallback
C'default'
Dhandle_message
Attempts:
3 left
💡 Hint
Common Mistakes
Using the exchange name instead of the queue name in basic_consume.
Passing a string instead of a function as the callback.
5fill in blank
hard

Fill all three blanks to declare a queue, publish a message to the default exchange, and consume messages from the queue.

RabbitMQ
channel.queue_declare(queue=[1], durable=True)
channel.basic_publish(exchange=[2], routing_key=[3], body='Test Message')
channel.basic_consume(queue=[1], on_message_callback=process_message, auto_ack=True)
Drag options to blanks, or click blank then click option'
A'task_queue'
B''
D'amq.direct'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a named exchange instead of the empty string for the default exchange.
Mismatching queue name and routing key causing messages not to be delivered.

Practice

(1/5)
1. What is the main purpose of the default exchange in RabbitMQ?
easy
A. To route messages directly to queues by their name
B. To broadcast messages to all queues
C. To store messages temporarily
D. To encrypt messages before delivery

Solution

  1. Step 1: Understand the default exchange role

    The default exchange in RabbitMQ routes messages directly to queues using the queue name as the routing key.
  2. Step 2: Compare with other exchange types

    Unlike fanout or topic exchanges, the default exchange does not broadcast or filter messages; it sends directly to the named queue.
  3. Final Answer:

    To route messages directly to queues by their name -> Option A
  4. Quick Check:

    Default exchange routes by queue name = A [OK]
Hint: Default exchange uses queue name as routing key [OK]
Common Mistakes:
  • Thinking default exchange broadcasts messages
  • Assuming default exchange stores messages
  • Confusing default exchange with encryption
2. Which of the following is the correct way to publish a message using the default exchange in RabbitMQ?
easy
A. channel.basic_publish(exchange='my_exchange', routing_key='queue1', body='Hello')
B. channel.basic_publish(exchange='', routing_key='queue1', body='Hello')
C. channel.basic_publish(exchange='default', routing_key='queue1', body='Hello')
D. channel.basic_publish(exchange=None, routing_key='queue1', body='Hello')

Solution

  1. Step 1: Identify the default exchange syntax

    In RabbitMQ, the default exchange is identified by an empty string ('') as the exchange parameter.
  2. Step 2: Validate the options

    Only channel.basic_publish(exchange='', routing_key='queue1', body='Hello') uses exchange='' which correctly specifies the default exchange.
  3. Final Answer:

    channel.basic_publish(exchange='', routing_key='queue1', body='Hello') -> Option B
  4. Quick Check:

    Default exchange = empty string '' [OK]
Hint: Use empty string for default exchange in publish [OK]
Common Mistakes:
  • Using a named exchange instead of empty string
  • Using None instead of empty string
  • Assuming 'default' is a valid exchange name
3. Given the following code snippet, what will be the result?
channel.queue_declare(queue='task_queue')
channel.basic_publish(exchange='', routing_key='task_queue', body='Work item')
medium
A. The message is discarded because no exchange is specified
B. The message is sent to all queues bound to the default exchange
C. The message 'Work item' is sent to the 'task_queue' queue
D. An error occurs because the exchange name is empty

Solution

  1. Step 1: Analyze queue declaration

    The queue 'task_queue' is declared and ready to receive messages.
  2. Step 2: Analyze message publishing

    Publishing with exchange='' and routing_key='task_queue' sends the message directly to the 'task_queue' queue via the default exchange.
  3. Final Answer:

    The message 'Work item' is sent to the 'task_queue' queue -> Option C
  4. Quick Check:

    Default exchange routes to queue named by routing_key = D [OK]
Hint: Empty exchange + routing_key = queue name [OK]
Common Mistakes:
  • Thinking empty exchange causes error
  • Assuming message broadcasts to all queues
  • Believing message is discarded without exchange
4. You wrote this code but no messages arrive in the queue:
channel.basic_publish(exchange='default', routing_key='my_queue', body='Test')
What is the likely problem?
medium
A. The routing key must be empty when using default exchange
B. The message body must be bytes, not string
C. The queue 'my_queue' must be declared before publishing
D. The exchange name 'default' is invalid; use empty string for default exchange

Solution

  1. Step 1: Check exchange parameter

    The default exchange is identified by an empty string (''), not by the name 'default'. Using 'default' causes the message to be sent to a non-existent exchange.
  2. Step 2: Understand impact on message delivery

    Since the exchange 'default' does not exist, the message is not routed and does not reach the queue.
  3. Final Answer:

    The exchange name 'default' is invalid; use empty string for default exchange -> Option D
  4. Quick Check:

    Default exchange = empty string '' [OK]
Hint: Default exchange must be empty string, not 'default' [OK]
Common Mistakes:
  • Using 'default' as exchange name
  • Thinking routing key must be empty
  • Ignoring queue declaration requirement
5. You want to send a message to a queue named 'orders' without creating a custom exchange. Which of the following code snippets correctly uses the default exchange to achieve this?
hard
A. channel.basic_publish(exchange='', routing_key='orders', body='Order123')
B. channel.basic_publish(exchange='orders', routing_key='orders', body='Order123')
C. channel.basic_publish(exchange='amq.direct', routing_key='orders', body='Order123')
D. channel.basic_publish(exchange=None, routing_key='orders', body='Order123')

Solution

  1. Step 1: Identify default exchange usage

    The default exchange is always referenced by an empty string (''). This allows direct routing to the queue named by the routing key.
  2. Step 2: Evaluate options

    channel.basic_publish(exchange='', routing_key='orders', body='Order123') uses exchange='' and routing_key='orders', which correctly sends the message to the 'orders' queue without needing a custom exchange.
  3. Final Answer:

    channel.basic_publish(exchange='', routing_key='orders', body='Order123') -> Option A
  4. Quick Check:

    Default exchange = empty string '' [OK]
Hint: Use empty string exchange to send directly to queue [OK]
Common Mistakes:
  • Using queue name as exchange name
  • Using None instead of empty string
  • Confusing default exchange with 'amq.direct'
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard