Bird
0
0

Given the following Kafka producer code snippet, what will be the partition number if the topic has 3 partitions?

medium📝 Predict Output Q4 of 15
Kafka - Producers
Given the following Kafka producer code snippet, what will be the partition number if the topic has 3 partitions?
String key = "user123";
String value = "purchase";
ProducerRecord record = new ProducerRecord<>("orders", key, value);
// Assume default partitioner is used
int partition = record.partition();
APartition 1
BPartition number is null until sent
CPartition 2
DPartition 0
Step-by-Step Solution
Solution:
  1. Step 1: Understand when partition is assigned

    Partition is assigned by the Kafka producer's partitioner during send, not when creating the record.
  2. Step 2: Check record.partition() before sending

    Calling partition() on a new record returns null if partition not explicitly set.
  3. Final Answer:

    Partition number is null until sent -> Option B
  4. Quick Check:

    Partition assigned on send, not on record creation [OK]
Quick Trick: Partition is assigned when sending, not on record creation [OK]
Common Mistakes:
  • Assuming partition is set on record creation
  • Confusing key hash with partition number
  • Expecting partition() to return a number before send

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes