Bird
0
0

Examine the following Kafka consumer poll loop snippet:

medium📝 Debug Q6 of 15
Kafka - Consumers
Examine the following Kafka consumer poll loop snippet:
while (true) {
  var records = consumer.poll(100);
  for (var record : records) {
    System.out.println(record.value());
  }
}

What is the primary issue with this code?
AThe for-each loop should iterate over consumer.records() instead of records.
BThe loop does not commit offsets after processing records.
CThe poll method is called with an int instead of a Duration object.
DThe code is missing a break statement to exit the loop.
Step-by-Step Solution
Solution:
  1. Step 1: Check the poll method signature

    Since Kafka 2.0, poll() requires a Duration parameter, not an int.
  2. Step 2: Identify the incorrect argument type

    Passing an int (100) instead of Duration.ofMillis(100) causes a compilation error.
  3. Step 3: Other parts of the code are syntactically correct

    The for-each loop and print statement are valid.
  4. Final Answer:

    The poll method is called with an int instead of a Duration object. correctly identifies the error.
  5. Quick Check:

    poll() requires Duration, not int [OK]
Quick Trick: Always use Duration with poll() method [OK]
Common Mistakes:
  • Passing an integer directly to poll()
  • Ignoring the required Duration parameter
  • Assuming poll() returns a single record

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes