Bird
0
0

Identify the error in this Kafka Producer code snippet:

medium📝 Debug Q14 of 15
Kafka - Producers
Identify the error in this Kafka Producer code snippet:
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("key.serializer", "StringSerializer");
props.put("value.serializer", "StringSerializer");
KafkaProducer producer = new KafkaProducer<>(props);
ProducerRecord record = new ProducerRecord<>("topic", "key", "value");
producer.send(record);
producer.close();
AMissing topic name in ProducerRecord
BProperties object not initialized
CProducer not closed properly
DIncorrect serializer class names
Step-by-Step Solution
Solution:
  1. Step 1: Check serializer class names in properties

    The serializer values must be full class names like "org.apache.kafka.common.serialization.StringSerializer".
  2. Step 2: Verify other code parts

    Topic name is present, producer is closed properly, and properties object is initialized correctly.
  3. Final Answer:

    Incorrect serializer class names -> Option D
  4. Quick Check:

    Serializer class names must be full package names [OK]
Quick Trick: Use full serializer class names with package [OK]
Common Mistakes:
  • Using short class names without package
  • Forgetting to close producer
  • Leaving topic name empty

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes