Bird
0
0

Given the following code snippet, what topics will the consumer be subscribed to?

medium📝 Predict Output Q4 of 15
Kafka - Consumers
Given the following code snippet, what topics will the consumer be subscribed to?
consumer.subscribe(Arrays.asList("topicA", "topicB"));
consumer.subscribe(Collections.singletonList("topicC"));
AThe consumer subscribes only to "topicA" and "topicB".
BThe consumer subscribes to "topicA", "topicB", and "topicC".
CThe consumer subscribes only to "topicC".
DThe consumer subscribes to none of the topics.
Step-by-Step Solution
Solution:
  1. Step 1: Understand how subscribe() works when called multiple times

    Each call to subscribe() replaces the previous subscription list; it does not add to it.
  2. Step 2: Analyze the code calls

    The first call subscribes to "topicA" and "topicB". The second call overwrites this and subscribes only to "topicC".
  3. Final Answer:

    The consumer subscribes only to "topicC". -> Option C
  4. Quick Check:

    subscribe() overwrites previous topics [OK]
Quick Trick: Multiple subscribe() calls overwrite previous subscriptions [OK]
Common Mistakes:
  • Assuming subscriptions add up instead of replacing
  • Thinking consumer listens to all topics from all calls
  • Ignoring that last subscribe() call is effective

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes