Bird
0
0

What will be the output of this Kafka consumer poll loop snippet if no messages are available?

medium📝 Predict Output Q4 of 15
Kafka - Consumers
What will be the output of this Kafka consumer poll loop snippet if no messages are available?
while (true) {
  var records = consumer.poll(Duration.ofMillis(100));
  System.out.println(records.count());
  break;
}
A0
Bnull
CException thrown
DInfinite loop with no output
Step-by-Step Solution
Solution:
  1. Step 1: Understand poll() returns empty records if none available

    If no messages are available, poll() returns an empty ConsumerRecords object, not null or exception.
  2. Step 2: Check records.count() output

    Calling count() on empty records returns 0, which is printed before break ends loop.
  3. Final Answer:

    0 -> Option A
  4. Quick Check:

    poll() empty records count = 0 [OK]
Quick Trick: poll() returns empty records, count() is zero if no messages [OK]
Common Mistakes:
  • Expecting null instead of empty records
  • Assuming exception on no messages
  • Thinking loop runs infinitely without break

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes