What is Apache Kafka used for in Spring Boot applications?
Apache Kafka is used as a messaging system to send and receive streams of data between different parts of an application or between different applications, enabling real-time data processing.
Click to reveal answer
beginner
What annotation is used in Spring Boot to create a Kafka listener?
The @KafkaListener annotation is used to mark a method as a listener for Kafka messages on a specified topic.
Click to reveal answer
beginner
How do you send a message to a Kafka topic in Spring Boot?
You use the KafkaTemplate class's send() method, specifying the topic name and the message to send.
Click to reveal answer
beginner
What is the role of application.properties in Kafka integration with Spring Boot?
It holds configuration settings like Kafka server addresses, topic names, and consumer group IDs to connect and communicate with Kafka brokers.
Click to reveal answer
intermediate
Why is it important to handle message serialization and deserialization in Kafka integration?
Because Kafka sends data as bytes, serialization converts objects to bytes when sending, and deserialization converts bytes back to objects when receiving, ensuring data is correctly understood.
Click to reveal answer
Which Spring Boot annotation is used to listen to Kafka messages?
A@KafkaProducer
B@KafkaListener
C@KafkaConsumer
D@KafkaHandler
✗ Incorrect
The @KafkaListener annotation marks a method to receive messages from Kafka topics.
What class do you use to send messages to Kafka in Spring Boot?
AKafkaTemplate
BKafkaSender
CKafkaProducerClient
DKafkaMessageSender
✗ Incorrect
KafkaTemplate is the Spring Boot class used to send messages to Kafka topics.
Where do you configure Kafka server addresses in a Spring Boot app?
Aapplication.yml
Bbootstrap.properties
CKafkaConfig.java
Dapplication.properties
✗ Incorrect
Kafka server addresses and other settings are typically set in application.properties.
What does Kafka use to group consumers for load balancing?
ABroker ID
BTopic Partition
CConsumer Group ID
DProducer ID
✗ Incorrect
Consumer Group ID groups consumers so they share message consumption.
Why must messages be serialized before sending to Kafka?
ATo convert data into bytes for transmission
BTo encrypt the message
CTo compress the message
DTo validate the message format
✗ Incorrect
Serialization converts data into bytes so Kafka can transmit it.
Explain how to set up a basic Kafka producer and consumer in Spring Boot.
Think about how you send and listen to messages and where you put settings.
You got /4 concepts.
Describe why serialization and deserialization are important in Kafka integration.
Consider how data travels between apps in Kafka.
You got /4 concepts.
Practice
(1/5)
1. What is the primary purpose of @KafkaListener in a Spring Boot application?
easy
A. To listen and consume messages from a Kafka topic
B. To send messages to a Kafka topic
C. To configure Kafka broker settings
D. To create new Kafka topics automatically
Solution
Step 1: Understand the role of @KafkaListener
This annotation marks a method to receive messages from a Kafka topic automatically.
Step 2: Differentiate sending and listening
Sending messages is done with KafkaTemplate, not @KafkaListener.
Final Answer:
To listen and consume messages from a Kafka topic -> Option A
What will be printed if a message "Hello Kafka" is sent to myTopic?
medium
A. Received: Hello Kafka
B. Hello Kafka
C. Received message: Hello Kafka
D. Error: No message received
Solution
Step 1: Analyze the listener method output
The method prints "Received: " concatenated with the message string.
Step 2: Substitute the message value
Message is "Hello Kafka", so output is "Received: Hello Kafka".
Final Answer:
Received: Hello Kafka -> Option A
Quick Check:
Prints "Received: " + message [OK]
Hint: Check print statement format carefully [OK]
Common Mistakes:
Ignoring the prefix in the print statement
Assuming only the message prints
Confusing topic name with output
4. Consider this Spring Boot Kafka configuration snippet:
@Bean
public NewTopic topic() {
return new NewTopic("testTopic", 1, (short) 1);
}
What is the likely error if the application fails to create the topic?
medium
A. Incorrect topic name format
B. Missing Kafka broker connection settings
C. NewTopic requires 3 parameters but only 2 provided
D. NewTopic bean must be annotated with @KafkaListener
Solution
Step 1: Understand NewTopic bean usage
NewTopic defines topic name, partitions, and replication factor correctly here.
Step 2: Identify common cause for topic creation failure
Failure usually happens if Kafka broker connection settings are missing or incorrect.
Final Answer:
Missing Kafka broker connection settings -> Option B
Quick Check:
Broker settings needed for topic creation [OK]
Hint: Check Kafka broker config if topic creation fails [OK]
Common Mistakes:
Thinking @KafkaListener is needed on NewTopic bean
Assuming topic name format is invalid
Miscounting NewTopic constructor parameters
5. You want to create a Spring Boot Kafka consumer that listens to two topics: topicA and topicB. Which of the following @KafkaListener annotations correctly achieves this?
hard
A. @KafkaListener(topics = "topicA, topicB")
B. @KafkaListener(topic = "topicA,topicB")
C. @KafkaListener(topics = {"topicA", "topicB"})
D. @KafkaListener(topics = ["topicA", "topicB"])
Solution
Step 1: Recall correct syntax for multiple topics
Spring Boot expects an array of strings for multiple topics: topics = {"topicA", "topicB"}.
Step 2: Evaluate other options
@KafkaListener(topic = "topicA,topicB") uses wrong attribute name and comma string, C uses string with comma but not array, D uses invalid syntax with square brackets.
Final Answer:
@KafkaListener(topics = {"topicA", "topicB"}) -> Option C
Quick Check:
Multiple topics require array syntax [OK]
Hint: Use curly braces for multiple topics in @KafkaListener [OK]
Common Mistakes:
Using comma-separated string instead of array
Wrong attribute name 'topic' instead of 'topics'
Using square brackets instead of curly braces
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.