A Kafka Consumer reads data from Kafka topics. It subscribes to topics and processes the messages sent by producers.
Click to reveal answer
beginner
What does 'subscribe()' method do in Kafka Consumer API?
The subscribe() method tells the consumer which topics to listen to. It allows the consumer to receive messages from those topics.
Click to reveal answer
beginner
Explain what 'poll()' method does in Kafka Consumer API.
The poll() method fetches records from the Kafka broker. It waits for new messages and returns them for processing.
Click to reveal answer
intermediate
Why is committing offsets important in Kafka Consumer?
Committing offsets tells Kafka which messages have been processed. This helps avoid processing the same message twice after a restart.
Click to reveal answer
intermediate
What is the difference between automatic and manual offset commit?
Automatic commit happens periodically without user control, while manual commit lets the user decide when to save the offset after processing messages.
Click to reveal answer
What method does a Kafka Consumer use to start receiving messages from a topic?
Asubscribe()
Bconnect()
Csend()
Dcommit()
✗ Incorrect
The subscribe() method is used to tell the consumer which topics to listen to.
Which method fetches messages from Kafka for the consumer to process?
Aread()
Bfetch()
Creceive()
Dpoll()
✗ Incorrect
poll() is the method that retrieves messages from Kafka.
Why do consumers commit offsets?
ATo send messages to Kafka
BTo mark messages as processed
CTo subscribe to topics
DTo create new topics
✗ Incorrect
Committing offsets marks messages as processed to avoid reprocessing.
What happens if a consumer does not commit offsets?
AIt may reprocess messages after restart
BIt will lose messages permanently
CIt cannot subscribe to topics
DIt will send duplicate messages
✗ Incorrect
Without committing offsets, the consumer may read the same messages again after restarting.
Which of these is a valid way to control offset commits?
AProducer acknowledgments
BAutomatic topic creation
CManual commit
DBroker replication
✗ Incorrect
Manual commit lets the consumer decide when to save offsets.
Describe the basic steps a Kafka Consumer takes to read messages from a topic.
Think about how the consumer connects, gets data, and remembers progress.
You got /4 concepts.
Explain why offset management is important in Kafka Consumer API.
Consider what happens if the consumer restarts.
You got /4 concepts.
Practice
(1/5)
1. What is the primary purpose of the Kafka Consumer API?
easy
A. To send messages to Kafka topics
B. To delete Kafka topics
C. To create Kafka topics
D. To read messages from Kafka topics
Solution
Step 1: Understand Kafka Consumer API role
The Consumer API is designed to read or consume messages from Kafka topics.
Step 2: Differentiate from Producer API
The Producer API sends messages, while Consumer API reads them.
Final Answer:
To read messages from Kafka topics -> Option D
Quick Check:
Consumer API = Read messages [OK]
Hint: Consumer API reads messages, Producer API sends messages [OK]
Common Mistakes:
Confusing Consumer API with Producer API
Thinking Consumer API creates or deletes topics
Mixing up reading and writing operations
2. Which of the following is the correct way to create a Kafka consumer in Java?
5. You want to process messages from multiple topics and commit offsets manually after processing. Which code snippet correctly sets up the Kafka consumer for this use case?
hard
A. props.put("enable.auto.commit", "false");
consumer.subscribe(Arrays.asList("topicA", "topicB"));
while (true) {
ConsumerRecords records = consumer.poll(Duration.ofMillis(1000));
for (ConsumerRecord record : records) {
process(record.value());
}
consumer.commitSync();
}
B. props.put("enable.auto.commit", "true");
consumer.subscribe(Collections.singletonList("topicA"));
while (true) {
ConsumerRecords records = consumer.poll(Duration.ofMillis(1000));
for (ConsumerRecord record : records) {
process(record.value());
}
}
C. props.put("enable.auto.commit", "false");
consumer.assign(Arrays.asList(new TopicPartition("topicA", 0)));
consumer.subscribe(Arrays.asList("topicB"));
while (true) {
ConsumerRecords records = consumer.poll(Duration.ofMillis(1000));
for (ConsumerRecord record : records) {
process(record.value());
}
consumer.commitAsync();
}
D. props.put("enable.auto.commit", "false");
consumer.subscribe(Arrays.asList("topicA", "topicB"));
while (true) {
ConsumerRecords records = consumer.poll(Duration.ofMillis(1000));
for (ConsumerRecord record : records) {
process(record.value());
}
}
Solution
Step 1: Disable auto commit for manual offset control
Setting "enable.auto.commit" to "false" allows manual offset commits.
Step 2: Subscribe to multiple topics correctly
Use consumer.subscribe(Arrays.asList("topicA", "topicB")) to listen to multiple topics.
Step 3: Commit offsets after processing
Calling consumer.commitSync() after processing ensures offsets are saved manually.
Final Answer:
Code snippet with manual commit, multiple topic subscription, and commitSync() -> Option A
Quick Check:
Manual commit + multiple topics = props.put("enable.auto.commit", "false");
consumer.subscribe(Arrays.asList("topicA", "topicB"));
while (true) {
ConsumerRecords records = consumer.poll(Duration.ofMillis(1000));
for (ConsumerRecord record : records) {
process(record.value());
}
consumer.commitSync();
} [OK]
Hint: Disable auto commit and call commitSync() after processing [OK]
Common Mistakes:
Using auto commit when manual commit is needed
Mixing assign() and subscribe() incorrectly
Forgetting to commit offsets after processing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.