Bird
Raised Fist0

What will happen when the following Java Kafka producer code is executed?

medium📝 Predict Output Q4 of Q15
Kafka - with Java/Python
What will happen when the following Java Kafka producer code is executed?
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
KafkaProducer producer = new KafkaProducer<>(props);
ProducerRecord record = new ProducerRecord<>("test-topic", "key1", "value1");
producer.send(record);
producer.close();
AThe message will be sent but the producer will not close properly.
BThe code will throw a serialization error at runtime.
CThe producer will fail to connect due to missing broker configuration.
DThe message with key 'key1' and value 'value1' is sent to 'test-topic' successfully.
Step-by-Step Solution
Solution:
  1. Step 1: Check configuration

    Bootstrap servers and serializers are correctly set.
  2. Step 2: Analyze send and close

    Producer sends the record and then closes cleanly.
  3. Step 3: Confirm no errors expected

    No errors in serialization or connection expected.
  4. Final Answer:

    The message with key 'key1' and value 'value1' is sent to 'test-topic' successfully. -> Option D
  5. Quick Check:

    Proper config and close means successful send. [OK]
Quick Trick: Correct serializers and close ensure successful send. [OK]
Common Mistakes:
MISTAKES
  • Assuming serialization error without cause
  • Forgetting to close producer
  • Misconfiguring bootstrap.servers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes