Hint: Use bootstrap_servers='host:port' exactly [OK]
Common Mistakes:
Omitting quotes around topic name
Using wrong keyword like servers or bootstrap
Passing topic name without quotes
3.
What will be the output of this code snippet if the Kafka topic test has one message with value b'hello'?
from kafka import KafkaConsumer
consumer = KafkaConsumer("test", bootstrap_servers='localhost:9092', auto_offset_reset='earliest', group_id='group1')
for msg in consumer:
print(msg.value.decode('utf-8'))
break
medium
A. hello
B. b'hello'
C. msg.value
D. Error: decode method not found
Solution
Step 1: Understand message value type
Kafka messages are bytes, so msg.value is b'hello'.
Step 2: Decode bytes to string
Calling decode('utf-8') converts b'hello' to 'hello' string, which is printed.
Final Answer:
hello -> Option A
Quick Check:
Decoded bytes print string [OK]
Hint: Decode bytes with decode('utf-8') to get string [OK]
Common Mistakes:
Printing raw bytes without decoding
Trying to decode string instead of bytes
Expecting b'hello' printed as string
4.
Identify the error in this Kafka consumer code snippet:
from kafka import KafkaConsumer
consumer = KafkaConsumer('topic1', bootstrap_servers='localhost:9092')
for message in consumer:
print(message.value.decode())
consumer.close()
medium
A. decode() requires an encoding argument
B. The consumer.close() is called outside the loop and never reached
C. Missing group_id parameter in KafkaConsumer
D. The for loop should use consumer.poll() instead
Solution
Step 1: Analyze loop and close call
The for loop on consumer is infinite; code never reaches consumer.close() after loop.
Step 2: Understand consumer.close() usage
To close consumer properly, loop must break or use context manager; otherwise close() is unreachable.
Final Answer:
The consumer.close() is called outside the loop and never reached -> Option B
Quick Check:
Infinite loop blocks close() call [OK]
Hint: Infinite loops prevent code after them from running [OK]
Common Mistakes:
Assuming decode() needs encoding argument
Thinking group_id is mandatory
Confusing poll() with iteration
5.
You want to consume messages from two Kafka topics topicA and topicB using one Python KafkaConsumer instance. Which code snippet correctly sets this up?
from kafka import KafkaConsumer
consumer = KafkaConsumer(_____)
for msg in consumer:
print(f"Topic: {msg.topic}, Message: {msg.value.decode()}")
hard
A. ["topicA", "topicB"], bootstrap_servers='localhost:9092', auto_offset_reset='earliest', group_id='group1'
B. "topicA,topicB", bootstrap_servers='localhost:9092', group_id='group1'
C. "topicA" and "topicB", bootstrap_servers='localhost:9092'
D. "topicA"; "topicB", bootstrap_servers='localhost:9092', group_id='group1'
Solution
Step 1: Check how to subscribe to multiple topics
KafkaConsumer accepts a list of topic names to subscribe to multiple topics.
Step 2: Verify correct syntax for multiple topics
["topicA", "topicB"], bootstrap_servers='localhost:9092', auto_offset_reset='earliest', group_id='group1' correctly passes a list ["topicA", "topicB"] and required parameters.
Final Answer:
["topicA", "topicB"], bootstrap_servers='localhost:9092', auto_offset_reset='earliest', group_id='group1' -> Option A
Quick Check:
List of topics subscribes to multiple topics [OK]
Hint: Use list of topic names to consume multiple topics [OK]
Common Mistakes:
Passing topics as comma-separated string
Using logical operators like and or ; between topics
Omitting group_id for consumer group
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.