Bird
Raised Fist0

What will be the output of the following Java Kafka producer code snippet?

medium📝 Predict Output Q13 of Q15
Kafka - with Java/Python
What will be the output of the following Java Kafka producer code snippet?
import java.util.Properties;
import org.apache.kafka.clients.producer.*;
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");

Producer<String, String> producer = new KafkaProducer<>(props);
ProducerRecord<String, String> record = new ProducerRecord<>("my-topic", "key1", "value1");
producer.send(record);
producer.close();
System.out.println("Message sent");
AMessage sent
BCompilation error due to missing imports
CRuntime error due to missing Kafka server
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the code flow

    The code creates a producer, sends a message, closes the producer, then prints "Message sent".
  2. Step 2: Consider output behavior

    Assuming Kafka server is running and imports are correct, the print statement executes after send and close.
  3. Final Answer:

    Message sent -> Option A
  4. Quick Check:

    Print after send and close = "Message sent" [OK]
Quick Trick: Print statement runs after send, shows "Message sent" [OK]
Common Mistakes:
MISTAKES
  • Assuming send() blocks until message delivered
  • Ignoring that print runs regardless of Kafka server
  • Confusing compile errors with runtime issues

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes