0
0
Kafkadevops~20 mins

Consumer configuration 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 configuration code?
Consider the following Kafka consumer configuration snippet in Java. What will be the value of props.getProperty("enable.auto.commit") after execution?
Kafka
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "test-group");
props.put("enable.auto.commit", "false");
props.put("auto.commit.interval.ms", "1000");
String autoCommit = props.getProperty("enable.auto.commit");
A"true"
B"false"
Cnull
DThrows NullPointerException
Attempts:
2 left
💡 Hint
Check the value assigned to the key "enable.auto.commit" in the properties.
Predict Output
intermediate
2:00remaining
What will be the output of this Kafka consumer poll timeout code?
Given the following Kafka consumer poll call, what is the type and meaning of the returned value?
Kafka
Consumer<String, String> consumer = new KafkaConsumer<>(props);
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(500));
System.out.println(records.count());
AThrows TimeoutException
BAlways zero because poll timeout is too short
CNumber of partitions assigned to the consumer
DNumber of records fetched in 500 milliseconds
Attempts:
2 left
💡 Hint
The poll method returns records fetched within the timeout duration.
Predict Output
advanced
2:00remaining
What error does this Kafka consumer configuration cause?
Analyze the following Kafka consumer configuration snippet. What error will occur when creating the consumer?
Kafka
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "test-group");
props.put("enable.auto.commit", "maybe");
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
AIllegalArgumentException due to invalid enable.auto.commit value
BTimeoutException on consumer creation
CNullPointerException because group.id is missing
DNo error, consumer created successfully
Attempts:
2 left
💡 Hint
The enable.auto.commit property expects "true" or "false" only.
🧠 Conceptual
advanced
2:00remaining
How many partitions will the consumer subscribe to after this code?
Given a Kafka topic with 4 partitions, what is the number of partitions assigned to the consumer after this subscription code?
Kafka
consumer.subscribe(Collections.singletonList("my-topic"));
Set<TopicPartition> assigned = consumer.assignment();
int count = assigned.size();
System.out.println(count);
A0
B4
C1
DThrows IllegalStateException
Attempts:
2 left
💡 Hint
Partitions are assigned only after poll() is called.
Predict Output
expert
3:00remaining
What is the output of this Kafka consumer manual commit code?
Consider the following code snippet for manual offset commit. What will be printed?
Kafka
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "test-group");
props.put("enable.auto.commit", "false");
props.put("auto.offset.reset", "earliest");
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props, new StringDeserializer(), new StringDeserializer());
consumer.subscribe(List.of("my-topic"));
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
for (ConsumerRecord<String, String> record : records) {
    System.out.println(record.offset() + ":" + record.value());
}
consumer.commitSync();
System.out.println("Committed offsets");
APrints nothing and then 'Committed offsets'
BPrints records but never prints 'Committed offsets'
CPrints all records with offsets and values, then 'Committed offsets'
DThrows CommitFailedException
Attempts:
2 left
💡 Hint
Manual commit commits offsets after processing records.