0
0
Kafkadevops~20 mins

Java consumer client in Kafka - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kafka Java Consumer 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 consumer poll loop?
Consider the following Java Kafka consumer code snippet. What will be printed to the console when the consumer polls messages from the topic?
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(1000));
for (ConsumerRecord<String, String> record : records) {
    System.out.println("Received message: " + record.value());
}
consumer.close();
ACompilation error due to missing imports
B
Received message: Hello
Received message: World
CNo output because poll returns empty records
DRuntime exception due to missing group.id property
Attempts:
2 left
💡 Hint
Assume the topic 'my-topic' has two messages: "Hello" and "World".
🧠 Conceptual
intermediate
1:30remaining
What does the 'auto.offset.reset' property control in a Kafka consumer?
In Kafka consumer configuration, what is the role of the 'auto.offset.reset' property?
AIt controls what happens when there is no initial offset or the current offset is invalid
BIt sets the maximum number of messages to fetch in one poll
CIt defines the timeout for consumer session heartbeat
DIt specifies the serializer class for message keys
Attempts:
2 left
💡 Hint
Think about what happens if the consumer group is new or the offset is lost.
🔧 Debug
advanced
2:30remaining
Why does this Kafka consumer code throw a SerializationException?
Examine the code below. 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 property

KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(Collections.singletonList("topic1"));
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
ABecause the value.deserializer property is missing, so Kafka cannot deserialize message values
BBecause the group.id is invalid and causes consumer failure
CBecause the topic name is incorrect and causes deserialization failure
DBecause the poll timeout is too short and causes serialization error
Attempts:
2 left
💡 Hint
Check the deserializer properties carefully.
📝 Syntax
advanced
1:30remaining
Which option correctly creates a Kafka consumer with generic types for String keys and Integer values?
Select the correct Java code snippet that creates a KafkaConsumer instance with String keys and Integer values.
AKafkaConsumer<String, Integer> consumer = new KafkaConsumer<String, Integer>(props);
BKafkaConsumer<String, Integer> consumer = new KafkaConsumer(props);
CKafkaConsumer<String, Integer> consumer = new KafkaConsumer<>(props);
DKafkaConsumer<String, Integer> consumer = new KafkaConsumer<Integer, String>(props);
Attempts:
2 left
💡 Hint
Consider Java's diamond operator and generic type matching.
🚀 Application
expert
3:00remaining
How many partitions will the consumer group consume concurrently with this configuration?
Given a Kafka topic with 4 partitions and a consumer group with 3 consumers, how many partitions will each consumer consume concurrently?
AEach consumer will consume 4 partitions concurrently
BEach consumer will consume exactly 1 partition
COne consumer will consume all 4 partitions, others none
DTwo consumers will consume 1 partition each, and one consumer will consume 2 partitions
Attempts:
2 left
💡 Hint
Partitions are divided among consumers in a group, but not always evenly.