Bird
0
0

Given the following Kafka consumer code snippet, what will be the output if the consumer is subscribed to topic "news" and there are two messages "Hello" and "World" in the topic?

medium📝 Predict Output Q13 of 15
Kafka - Consumers
Given the following Kafka consumer code snippet, what will be the output if the consumer is subscribed to topic "news" and there are two messages "Hello" and "World" in the topic?
consumer.subscribe(["news"])
records = consumer.poll(timeout_ms=1000)
for record in records:
    print(record.value)
A['Hello', 'World']
BHello\nWorld
CHelloWorld
DNo output because poll() is missing a loop
Step-by-Step Solution
Solution:
  1. Step 1: Understand subscribe() and poll() behavior

    The consumer subscribes to "news" and calls poll() once, which fetches available messages.
  2. Step 2: Analyze the for loop printing each record

    The loop prints each message value on its own line, so "Hello" and "World" appear separately.
  3. Final Answer:

    Hello\nWorld -> Option B
  4. Quick Check:

    poll() returns messages, loop prints each [OK]
Quick Trick: poll() returns messages; loop prints each on new line [OK]
Common Mistakes:
  • Thinking poll() returns a list printed directly
  • Assuming messages print concatenated without newline
  • Believing poll() needs a loop to work at all

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes