0
0
Kafkadevops~20 mins

Event streaming concept in Kafka - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kafka Event Streaming 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 this Kafka producer code in Java that sends a message to a topic named test-topic. What will be printed on 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", "Hello Kafka");
producer.send(record, (metadata, exception) -> {
    if (exception == null) {
        System.out.println("Sent to partition " + metadata.partition());
    } else {
        System.out.println("Error sending message");
    }
});
producer.close();
ASent to partition 0
BError sending message
CSent to partition 1
DNo output
Attempts:
2 left
💡 Hint

Assume the topic test-topic has only one partition.

🧠 Conceptual
intermediate
1:30remaining
What does Kafka's consumer group ensure?

In Kafka, what is the main purpose of a consumer group?

ATo allow multiple consumers to read the same message independently
BTo distribute partitions among consumers for parallel processing
CTo store messages permanently in Kafka topics
DTo encrypt messages during transmission
Attempts:
2 left
💡 Hint

Think about how Kafka balances workload among consumers.

🔧 Debug
advanced
2:30remaining
Why does this Kafka consumer code fail to receive messages?

Look at this Kafka consumer code snippet. It never prints any consumed messages. What is the cause?

Kafka
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "group1");
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(Collections.singletonList("my-topic"));

while (true) {
    ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
    for (ConsumerRecord<String, String> record : records) {
        System.out.println("Received: " + record.value());
    }
}
AThe consumer does not call consumer.close(), causing resource leak
BThe consumer group ID is missing, causing subscription failure
CThe consumer never commits offsets, so it reads no messages
DThe consumer does not call poll() correctly to fetch messages
Attempts:
2 left
💡 Hint

Check the poll method usage and its parameters.

📝 Syntax
advanced
1:30remaining
Which Kafka Streams code snippet compiles without error?

Which of these Kafka Streams Java code snippets correctly creates a stream from topic input-topic?

A
StreamsBuilder builder = new StreamsBuilder();
KStream&lt;String, String&gt; stream = builder.stream("input-topic");
B
StreamsBuilder builder = new StreamsBuilder()
KStream&lt;String, String&gt; stream = builder.stream("input-topic")
C
StreamsBuilder builder = new StreamsBuilder();
KStream&lt;String, String&gt; stream = builder.stream(input-topic);
D
StreamsBuilder builder = new StreamsBuilder();
KStream&lt;String, String&gt; stream = builder.stream('input-topic');
Attempts:
2 left
💡 Hint

Check for correct Java syntax and string literals.

🚀 Application
expert
2:00remaining
How many partitions will be assigned to each consumer in this group?

A Kafka topic has 6 partitions. There are 3 consumers in the same consumer group subscribed to this topic. How many partitions will each consumer get assigned?

AEach consumer gets 3 partitions
BEach consumer gets 1 partition
CEach consumer gets 2 partitions
DAll consumers get all 6 partitions
Attempts:
2 left
💡 Hint

Partitions are divided evenly among consumers in a group.