Bird
0
0

Which of the following Java code snippets correctly creates a Kafka ProducerRecord with both a key and a value?

easy📝 Syntax Q3 of 15
Kafka - Producers
Which of the following Java code snippets correctly creates a Kafka ProducerRecord with both a key and a value?
Anew ProducerRecord<>("key1", "value1");
Bnew ProducerRecord<>("topicName", "value1");
Cnew ProducerRecord<>("topicName", "key1", "value1");
Dnew ProducerRecord<>("topicName");
Step-by-Step Solution
Solution:
  1. Step 1: Understand ProducerRecord constructor

    The constructor ProducerRecord(String topic, K key, V value) requires topic, key, and value.
  2. Step 2: Analyze options

    new ProducerRecord<>("topicName", "key1", "value1"); correctly passes topic, key, and value. new ProducerRecord<>("topicName", "value1"); misses the key. new ProducerRecord<>("key1", "value1"); incorrectly uses key as topic. new ProducerRecord<>("topicName"); only passes topic.
  3. Final Answer:

    new ProducerRecord<>("topicName", "key1", "value1"); -> Option C
  4. Quick Check:

    Constructor signature matches new ProducerRecord<>("topicName", "key1", "value1"); [OK]
Quick Trick: ProducerRecord needs topic, key, and value in order [OK]
Common Mistakes:
  • Confusing key and value parameters
  • Omitting the topic parameter
  • Using wrong constructor overload

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes