Bird
0
0

Given this snippet, what will be printed?

medium📝 Predict Output Q5 of 15
Kafka - Consumers
Given this snippet, what will be printed?
while (true) {
  var records = consumer.poll(Duration.ofMillis(100));
  for (var record : records) {
    System.out.println(record.value());
  }
  break;
}

Assuming the topic has messages with values "Hello" and "World".
AException thrown
Bnull
CNo output
DHello\nWorld
Step-by-Step Solution
Solution:
  1. Step 1: Understand poll() fetches available messages

    poll() returns ConsumerRecords containing messages "Hello" and "World".
  2. Step 2: Loop prints each record's value

    The for loop prints each message value on a new line.
  3. Final Answer:

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

    poll() + for loop prints messages = Hello\nWorld [OK]
Quick Trick: For loop over records prints each message value [OK]
Common Mistakes:
  • Expecting null or no output
  • Forgetting to iterate over records
  • Assuming exception without cause

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes