A producer sends messages (data) to a Kafka topic. Think of it like someone writing letters and putting them into a mailbox for others to read.
Click to reveal answer
beginner
What does a consumer do in Kafka?
A consumer reads messages from a Kafka topic. It's like picking up letters from the mailbox and reading them.
Click to reveal answer
beginner
Why do we need topics in Kafka?
Topics organize messages by category or type. Imagine topics as different mailboxes for different kinds of letters.
Click to reveal answer
beginner
What is the first step to send a message in Kafka?
Create a producer, connect it to Kafka, and send a message to a topic.
Click to reveal answer
intermediate
How does a consumer know which message to read first?
Consumers read messages in the order they were sent to the topic, starting from the earliest or latest message depending on configuration.
Click to reveal answer
What is the main purpose of a Kafka producer?
ATo send messages to a topic
BTo read messages from a topic
CTo delete messages from a topic
DTo create topics
✗ Incorrect
A Kafka producer's job is to send messages to a topic.
Which component reads messages from a Kafka topic?
AProducer
BBroker
CConsumer
DZookeeper
✗ Incorrect
Consumers read messages from Kafka topics.
What is a Kafka topic best compared to in real life?
AA letter
BA mailbox
CA stamp
DA postman
✗ Incorrect
A topic is like a mailbox where messages are stored.
When producing the first message, what must you specify?
AThe message offset
BThe consumer group
CThe broker address only
DThe topic name
✗ Incorrect
You must specify the topic name to send the message to the right place.
How does a Kafka consumer decide where to start reading messages?
AFrom the earliest or latest message based on config
BRandomly
COnly the newest message
DOnly the oldest message
✗ Incorrect
Consumers can be configured to start reading from earliest or latest messages.
Explain how to produce and consume the first message in Kafka in simple terms.
Think about sending and receiving letters using mailboxes.
You got /4 concepts.
Describe the roles of producer, consumer, and topic in Kafka.
Imagine a mailing system with senders, mailboxes, and readers.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of producing a message in Kafka?
easy
A. To send data to a Kafka topic
B. To read data from a Kafka topic
C. To delete messages from a Kafka topic
D. To create a new Kafka topic
Solution
Step 1: Understand Kafka producing
Producing means sending messages to a Kafka topic as bytes.
Step 2: Differentiate producing from consuming
Consuming reads messages, but producing sends them.
Final Answer:
To send data to a Kafka topic -> Option A
Quick Check:
Producing = Sending messages [OK]
Hint: Producing means sending messages to Kafka topics [OK]
Common Mistakes:
Confusing producing with consuming
Thinking producing deletes messages
Assuming producing creates topics
2. Which of the following is the correct way to create a Kafka producer in Python using the kafka-python library?
easy
A. producer = KafkaConsumer(topic='mytopic')
B. producer = KafkaConsumer(bootstrap_servers='localhost:9092')
C. producer = KafkaProducer(topic='mytopic')
D. producer = KafkaProducer(bootstrap_servers='localhost:9092')
Solution
Step 1: Identify Kafka producer creation syntax
KafkaProducer is created with bootstrap_servers parameter specifying Kafka server address.
Step 2: Eliminate consumer and wrong parameters
KafkaConsumer is for consuming, not producing. Topic is not a parameter for producer creation.
Final Answer:
producer = KafkaProducer(bootstrap_servers='localhost:9092') -> Option D
Quick Check:
Producer uses KafkaProducer with bootstrap_servers [OK]
Hint: Producer uses KafkaProducer with bootstrap_servers [OK]
Common Mistakes:
Using KafkaConsumer instead of KafkaProducer
Passing topic during producer creation
Missing bootstrap_servers parameter
3. Consider this Python code snippet using kafka-python:
from kafka import KafkaProducer, KafkaConsumer
producer = KafkaProducer(bootstrap_servers='localhost:9092')
producer.send('test-topic', b'Hello Kafka')
producer.flush()
consumer = KafkaConsumer('test-topic', bootstrap_servers='localhost:9092', auto_offset_reset='earliest', consumer_timeout_ms=1000)
for msg in consumer:
print(msg.value.decode())
What will be the output when this code runs?
medium
A. Error: topic not found
B. Hello Kafka
C. No output, consumer times out
D. b'Hello Kafka'
Solution
Step 1: Analyze producer sending message
Producer sends bytes message b'Hello Kafka' to 'test-topic' and flushes to ensure delivery.
Step 2: Analyze consumer reading message
Consumer reads from 'test-topic' from earliest offset and decodes bytes to string before printing.
Final Answer:
Hello Kafka -> Option B
Quick Check:
Consumer prints decoded message string [OK]
Hint: Consumer decodes bytes to string before printing [OK]
Common Mistakes:
Expecting raw bytes output
Assuming consumer misses message due to timeout
Thinking topic does not exist
4. You wrote this code to consume messages but it raises an error:
consumer = KafkaConsumer('my-topic')
for message in consumer:
print(message.value.decode())
What is the most likely cause of the error?
medium
A. Using decode() on message.value which is already a string
B. Not calling consumer.poll() before iterating
C. Missing bootstrap_servers parameter when creating KafkaConsumer
D. Topic name should be a list, not a string
Solution
Step 1: Check KafkaConsumer creation requirements
KafkaConsumer requires bootstrap_servers parameter to connect to Kafka cluster.
Step 2: Validate other options
decode() is needed because message.value is bytes; consumer.poll() is optional; topic can be string or list.
Final Answer:
Missing bootstrap_servers parameter when creating KafkaConsumer -> Option C
Quick Check:
KafkaConsumer needs bootstrap_servers [OK]
Hint: Always specify bootstrap_servers when creating KafkaConsumer [OK]
Common Mistakes:
Omitting bootstrap_servers parameter
Assuming message.value is string without decoding
Thinking poll() is mandatory before iteration
5. You want to produce and consume the first message in a Kafka topic named 'orders'. Which sequence of steps correctly achieves this in Python using kafka-python?
hard
A. Create KafkaProducer with bootstrap_servers, send message as bytes, flush; create KafkaConsumer with bootstrap_servers and auto_offset_reset='earliest', then read and decode messages
B. Create KafkaConsumer first, then KafkaProducer; send message as string; consumer reads without decoding
C. Create KafkaProducer without bootstrap_servers, send string message; create KafkaConsumer with bootstrap_servers, read messages as bytes
D. Create KafkaProducer with topic parameter, send message; create KafkaConsumer without bootstrap_servers, read and decode messages
Solution
Step 1: Properly create KafkaProducer and send message
KafkaProducer must be created with bootstrap_servers; messages sent as bytes and flushed to ensure delivery.
Step 2: Properly create KafkaConsumer and read messages
KafkaConsumer needs bootstrap_servers and auto_offset_reset='earliest' to read from the first message; decode bytes to string when reading.
Final Answer:
Create KafkaProducer with bootstrap_servers, send message as bytes, flush; create KafkaConsumer with bootstrap_servers and auto_offset_reset='earliest', then read and decode messages -> Option A
Quick Check:
Correct producer and consumer setup with bytes and decoding [OK]
Hint: Producer sends bytes and flushes; consumer reads earliest and decodes [OK]
Common Mistakes:
Creating producer or consumer without bootstrap_servers
Sending string instead of bytes
Not setting auto_offset_reset to 'earliest'
Not decoding message bytes before printing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.