Bird
Raised Fist0

What will be the output of the following Python code using confluent-kafka producer?

medium📝 Predict Output Q13 of Q15
Kafka - with Java/Python
What will be the output of the following Python code using confluent-kafka producer?
from confluent_kafka import Producer

p = Producer({'bootstrap.servers': 'localhost:9092'})
p.produce('my_topic', key='key1', value='hello')
p.flush()
print('Message sent')
AError: No Kafka server found
Bhello
CNo output
DMessage sent
Step-by-Step Solution
Solution:
  1. Step 1: Understand the code flow

    The code creates a producer, sends a message to 'my_topic', waits for delivery with flush(), then prints 'Message sent'.
  2. Step 2: Identify the printed output

    The only print statement outputs the string 'Message sent'. The message sending is asynchronous but flush ensures completion before print.
  3. Final Answer:

    Message sent -> Option D
  4. Quick Check:

    flush() waits, then print runs [OK]
Quick Trick: flush() waits, print runs after sending [OK]
Common Mistakes:
MISTAKES
  • Expecting message value to print
  • Ignoring flush() effect
  • Assuming error without Kafka running

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes