Bird
0
0

Identify the error in this Python code snippet for publishing a message:

medium📝 Debug Q6 of 15
GCP - Cloud Pub/Sub
Identify the error in this Python code snippet for publishing a message:
from google.cloud import pubsub_v1
publisher = pubsub_v1.PublisherClient()
topic_path = 'projects/my-project/topics/my-topic'
publisher.publish(topic_path, data='Hello')
Adata parameter should be bytes, not string
BMissing import for SubscriberClient
Ctopic_path should be a Subscription path
Dpublish() method does not exist
Step-by-Step Solution
Solution:
  1. Step 1: Check data parameter type

    publish() requires data as bytes, but 'Hello' is a string.
  2. Step 2: Other options are unrelated

    SubscriberClient import is not needed here; topic_path is correct; publish() exists.
  3. Final Answer:

    data parameter should be bytes, not string -> Option A
  4. Quick Check:

    Data must be bytes for publish() [OK]
Quick Trick: Encode strings to bytes before publishing [OK]
Common Mistakes:
  • Using string instead of bytes
  • Confusing topic and subscription paths
  • Missing client imports unrelated to error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More GCP Quizzes