0
0
Kafkadevops~20 mins

Consumer API basics in Kafka - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kafka Consumer 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 consumer poll code?

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

Kafka
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "test-group");
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"));

ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
for (ConsumerRecord<String, String> record : records) {
    System.out.println(record.key() + ":" + record.value());
}
consumer.close();
AThrows a SerializationException due to missing deserializer configuration
BThrows a NullPointerException because poll duration is too short
CPrints all key:value pairs from 'my-topic' available within 100ms poll duration
DPrints nothing if no messages are available within 100ms poll duration
Attempts:
2 left
💡 Hint

Think about what happens if the topic has no messages ready during the poll.

🧠 Conceptual
intermediate
1:30remaining
What does the 'group.id' property control in Kafka consumer?

In Kafka consumer configuration, what is the role of the group.id property?

AIt sets the maximum number of messages to fetch per poll
BIt identifies the consumer group for load balancing and offset management
CIt specifies the Kafka broker address to connect to
DIt defines the serialization format for message keys
Attempts:
2 left
💡 Hint

Think about how Kafka manages multiple consumers reading from the same topic.

🔧 Debug
advanced
2:30remaining
Why does this Kafka consumer code cause a runtime error?

Examine the following Kafka consumer code snippet. Why does it throw a SerializationException at runtime?

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");
// Missing value.deserializer configuration

KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(Collections.singletonList("topic1"));
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
for (ConsumerRecord<String, String> record : records) {
    System.out.println(record.key() + ":" + record.value());
}
consumer.close();
ABecause the value.deserializer property is missing, Kafka cannot deserialize message values
BBecause the topic name is incorrect, causing topic not found error
CBecause the poll duration is too short, causing timeout exceptions
DBecause the group.id is invalid, causing consumer group join failure
Attempts:
2 left
💡 Hint

Check the deserializer configurations carefully.

📝 Syntax
advanced
1:30remaining
Which option correctly commits offsets synchronously in Kafka consumer?

Which of the following code snippets correctly commits offsets synchronously after processing records?

Aconsumer.commitAsync();
Bconsumer.commitSync(Duration.ofSeconds(1));
Cconsumer.commitSync();
Dconsumer.commitAsync(Duration.ofSeconds(1));
Attempts:
2 left
💡 Hint

Look for the method that commits offsets synchronously without parameters.

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

Suppose a Kafka topic has 6 partitions and there are 3 consumers in the same consumer group subscribed to this topic. How many partitions will each consumer be assigned?

AEach consumer will be assigned 2 partitions
BEach consumer will be assigned 3 partitions
COne consumer will get all 6 partitions, others get none
DPartitions are assigned randomly with no guarantee of distribution
Attempts:
2 left
💡 Hint

Think about how Kafka balances partitions among consumers in a group.