0
0
Kafkadevops~20 mins

Producer API basics in Kafka - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kafka Producer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Kafka Producer code snippet?

Consider the following Kafka Producer code in Java. What will be printed to the console?

Kafka
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<>("test-topic", "key1", "value1");

try {
    RecordMetadata metadata = producer.send(record).get();
    System.out.println("Sent to partition: " + metadata.partition());
} catch (Exception e) {
    System.out.println("Send failed");
}
producer.close();
ASend failed
BSent to partition: 0
CSent to partition: 1
DCompilation error
Attempts:
2 left
💡 Hint

By default, Kafka assigns messages to partition 0 if no custom partitioner is set and the topic has one partition.

🧠 Conceptual
intermediate
1:30remaining
Which property controls the Kafka Producer's acknowledgment behavior?

In Kafka Producer configuration, which property determines how many acknowledgments the producer requires the leader to receive before considering a request complete?

Aacks
Bbatch.size
Cretries
Dlinger.ms
Attempts:
2 left
💡 Hint

Think about the property that controls message durability guarantees.

🔧 Debug
advanced
2:00remaining
What error does this Kafka Producer code raise?

Examine the following Kafka Producer code snippet. What error will it raise when run?

Kafka
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
// Missing value.serializer property

Producer<String, String> producer = new KafkaProducer<>(props);

ProducerRecord<String, String> record = new ProducerRecord<>("test-topic", "key1", "value1");
producer.send(record);
producer.close();
AClassNotFoundException for StringSerializer
BNullPointerException at send()
CNo error, message sent successfully
Dorg.apache.kafka.common.KafkaException: Failed to construct kafka producer
Attempts:
2 left
💡 Hint

Check if all required serializer properties are set.

📝 Syntax
advanced
2:00remaining
Which option correctly creates a Kafka Producer with String key and value?

Choose the correct Java code snippet that creates a Kafka Producer with String key and value serializers.

A
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("key.serializer", new StringSerializer());
props.put("value.serializer", new StringSerializer());
Producer&lt;String, String&gt; producer = new KafkaProducer&lt;&gt;(props);
B
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("key.serializer", StringSerializer.class.getName());
props.put("value.serializer", StringSerializer.class.getName());
Producer&lt;String, String&gt; producer = new KafkaProducer&lt;&gt;(props);
C
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&lt;String, String&gt; producer = new KafkaProducer&lt;&gt;(props);
D
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("key.serializer", "StringSerializer");
props.put("value.serializer", "StringSerializer");
Producer&lt;String, String&gt; producer = new KafkaProducer&lt;&gt;(props);
Attempts:
2 left
💡 Hint

The serializer properties require the full class name as a string.

🚀 Application
expert
2:30remaining
How many messages are sent in this Kafka Producer loop?

Given the following Kafka Producer code, how many messages will be sent to the topic?

Kafka
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);

for (int i = 0; i < 5; i++) {
    ProducerRecord<String, String> record = new ProducerRecord<>("test-topic", "key" + i, "value" + i);
    producer.send(record);
    if (i == 2) {
        break;
    }
}
producer.close();
A3
B5
C2
D0
Attempts:
2 left
💡 Hint

Consider the effect of the break statement inside the loop.