Bird
Raised Fist0

Given the following code snippet, what will be printed if the topic test-topic has two messages with values "Hello" and "World"?

medium📝 Predict Output Q4 of Q15
Kafka - with Java/Python

Given the following code snippet, what will be printed if the topic test-topic has two messages with values "Hello" and "World"?

consumer.subscribe(Collections.singletonList("test-topic"));
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
for (ConsumerRecord<String, String> record : records) {
    System.out.println(record.value());
}
ACompilation error
BWorld\nHello
CHello\nWorld
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Understand subscription and polling

    The consumer subscribes to the topic and polls for messages, which returns all available records.
  2. Step 2: Analyze the loop output

    The loop prints each record's value in the order received, which is typically the order messages were produced: "Hello" then "World".
  3. Final Answer:

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

    poll() returns messages in order = D [OK]
Quick Trick: poll() returns messages in production order [OK]
Common Mistakes:
MISTAKES
  • Assuming reversed order of messages
  • Expecting no output if poll timeout is short
  • Confusing compile errors with runtime behavior

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes