Bird
Raised Fist0

Consider this Kafka consumer code snippet:

medium📝 Predict Output Q4 of Q15
Kafka - with Java/Python
Consider this Kafka consumer code snippet:
try {
  ConsumerRecords records = consumer.poll(Duration.ofMillis(500));
  // process records
} catch (TimeoutException e) {
  System.out.println("Polling timed out");
}

What happens if the poll() method throws a TimeoutException and it is not caught?
AThe exception propagates and may cause the consumer thread to terminate unexpectedly
BThe poll() call silently retries until successful
CThe consumer automatically reconnects and continues polling
DThe poll() method returns an empty ConsumerRecords object
Step-by-Step Solution
Solution:
  1. Step 1: Understand exception propagation

    If poll() throws a TimeoutException and it is not caught, the exception propagates up the call stack.
  2. Step 2: Consequences

    Uncaught exceptions can cause the consumer thread to terminate unexpectedly, potentially stopping message consumption.
  3. Step 3: Evaluate other options

    Options B, C, and D describe behaviors that do not happen automatically without explicit handling.
  4. Final Answer:

    The exception propagates and may cause the consumer thread to terminate unexpectedly -> Option A
  5. Quick Check:

    Uncaught exceptions propagate and can stop the thread [OK]
Quick Trick: Uncaught exceptions stop consumer thread unless handled [OK]
Common Mistakes:
MISTAKES
  • Assuming poll retries automatically on exceptions
  • Believing consumer reconnects without explicit code
  • Thinking poll returns empty records on exceptions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes