Bird
0
0

Given the following code snippet, what will be the output if the consumer seeks to offset 5 and reads one message?

medium📝 Predict Output Q13 of 15
Kafka - Consumers
Given the following code snippet, what will be the output if the consumer seeks to offset 5 and reads one message?
consumer.assign(Collections.singleton(topicPartition));
consumer.seek(topicPartition, 5);
ConsumerRecords records = consumer.poll(Duration.ofMillis(1000));
for (ConsumerRecord record : records) {
    System.out.println(record.offset());
    break;
}
A5
B0
C1
DException thrown
Step-by-Step Solution
Solution:
  1. Step 1: Understand seek behavior

    Calling seek sets the consumer's position to offset 5 in the assigned partition.
  2. Step 2: Poll and print offset

    The poll fetches messages starting at offset 5; printing record.offset() outputs 5.
  3. Final Answer:

    5 -> Option A
  4. Quick Check:

    seek to 5 then poll = offset 5 [OK]
Quick Trick: seek sets start offset; first polled message offset matches [OK]
Common Mistakes:
  • Assuming poll starts from offset 0
  • Expecting offset 1 instead of 5
  • Thinking seek causes an exception

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes