Bird
0
0

Given the following Kafka consumer code snippet in Python, what will be printed?

medium📝 Predict Output Q13 of 15
Kafka - Consumers
Given the following Kafka consumer code snippet in Python, what will be printed?
from kafka import KafkaConsumer

consumer = KafkaConsumer('test-topic', bootstrap_servers=['localhost:9092'])
for message in consumer:
    print(message.value.decode('utf-8'))
    break
AThe first message's value from 'test-topic' decoded as UTF-8
BAll messages from 'test-topic' printed continuously
CAn error because decode is not a method
DNo output because the loop breaks immediately
Step-by-Step Solution
Solution:
  1. Step 1: Understand the consumer loop behavior

    The for loop reads messages from 'test-topic' one by one.
  2. Step 2: Analyze the print and break statements

    The first message's value is decoded from bytes to string and printed, then the loop breaks immediately.
  3. Final Answer:

    The first message's value from 'test-topic' decoded as UTF-8 -> Option A
  4. Quick Check:

    Print first message then break = first message only [OK]
Quick Trick: Loop breaks after first message, so only one prints [OK]
Common Mistakes:
  • Thinking all messages print
  • Confusing decode method usage
  • Assuming no output due to break

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes