0
0
Kafkadevops~20 mins

Java producer client in Kafka - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kafka Java Producer Master
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 Java Kafka producer code snippet. 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");

KafkaProducer<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();
ASent to partition: 0
BSend failed
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 only one partition exists.

🧠 Conceptual
intermediate
1:00remaining
Which property is required to specify the Kafka broker address in a Java producer client?

When configuring a Kafka producer in Java, which property key must be set to specify the Kafka broker address?

Abootstrap.servers
Bbroker.address
Ckafka.broker
Dserver.address
Attempts:
2 left
💡 Hint

It usually contains the word 'bootstrap'.

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

Examine the following Java 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

KafkaProducer<String, String> producer = new KafkaProducer<>(props);
ProducerRecord<String, String> record = new ProducerRecord<>("test-topic", "key1", "value1");
producer.send(record);
producer.close();
ANullPointerException
BClassNotFoundException for StringSerializer
CNo error, message sent successfully
Dorg.apache.kafka.common.KafkaException: Missing value serializer
Attempts:
2 left
💡 Hint

Check if all required serializer properties are set.

📝 Syntax
advanced
1:30remaining
Which option fixes the syntax error in this Kafka producer code?

Identify the option that fixes the syntax error in the following Java Kafka producer snippet:

KafkaProducer producer = new KafkaProducer(props);
ProducerRecord record = new ProducerRecord<>("topic", "key", "value")
producer.send(record);
producer.close();
ARemove diamond operator and add semicolon after ProducerRecord line
BAdd diamond operator in constructor and semicolon after ProducerRecord line
CAdd semicolon after producer.send(record) line only
DNo changes needed, code is correct
Attempts:
2 left
💡 Hint

Check missing semicolons and proper generic usage.

🚀 Application
expert
2:00remaining
How many messages are sent by this Kafka producer code?

Given the following Java 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");

try (KafkaProducer<String, String> producer = new KafkaProducer<>(props)) {
    for (int i = 0; i < 5; i++) {
        ProducerRecord<String, String> record = new ProducerRecord<>("my-topic", "key" + i, "value" + i);
        producer.send(record);
        if (i == 2) {
            break;
        }
    }
}
A0
B5
C3
D2
Attempts:
2 left
💡 Hint

Look at the loop and the break statement carefully.