Bird
0
0

Consider this Python code snippet using kafka-python:

medium📝 Predict Output Q13 of 15
Kafka - Basics and Event Streaming
Consider this Python code snippet using kafka-python:
from kafka import KafkaProducer, KafkaConsumer

producer = KafkaProducer(bootstrap_servers='localhost:9092')
producer.send('test-topic', b'Hello Kafka')
producer.flush()

consumer = KafkaConsumer('test-topic', bootstrap_servers='localhost:9092', auto_offset_reset='earliest', consumer_timeout_ms=1000)
for msg in consumer:
    print(msg.value.decode())
What will be the output when this code runs?
AError: topic not found
BHello Kafka
CNo output, consumer times out
Db'Hello Kafka'
Step-by-Step Solution
Solution:
  1. Step 1: Analyze producer sending message

    Producer sends bytes message b'Hello Kafka' to 'test-topic' and flushes to ensure delivery.
  2. Step 2: Analyze consumer reading message

    Consumer reads from 'test-topic' from earliest offset and decodes bytes to string before printing.
  3. Final Answer:

    Hello Kafka -> Option B
  4. Quick Check:

    Consumer prints decoded message string [OK]
Quick Trick: Consumer decodes bytes to string before printing [OK]
Common Mistakes:
  • Expecting raw bytes output
  • Assuming consumer misses message due to timeout
  • Thinking topic does not exist

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes