Bird
0
0

What is wrong with this Kafka consumer poll loop?

medium📝 Debug Q7 of 15
Kafka - Consumers
What is wrong with this Kafka consumer poll loop?
while (true) {
  var records = consumer.poll(Duration.ofMillis(100));
  for (var record : records) {
    System.out.println(record.value());
  }
  consumer.close();
}
Afor loop cannot iterate over records.
Bpoll() should not use Duration argument.
CSystem.out.println() cannot print record values.
DCalling consumer.close() inside the loop stops polling prematurely.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze consumer.close() placement

    Calling close() inside the infinite loop closes the consumer after first poll, stopping further polling.
  2. Step 2: Confirm other code parts are correct

    poll() with Duration and for loop are valid; print statement is correct.
  3. Final Answer:

    Calling consumer.close() inside the loop stops polling prematurely. -> Option D
  4. Quick Check:

    close() inside loop = premature stop [OK]
Quick Trick: Close consumer after loop, not inside it [OK]
Common Mistakes:
  • Closing consumer inside poll loop
  • Misusing poll() argument
  • Incorrect for-each loop assumptions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes