Bird
0
0

Which of these is a valid way to subscribe a Kafka consumer to a single topic named "orders"?

easy📝 Conceptual Q2 of 15
Kafka - Consumers
Which of these is a valid way to subscribe a Kafka consumer to a single topic named "orders"?
Aconsumer.subscribe("orders");
Bconsumer.subscribe(Collections.singletonList("orders"));
Cconsumer.subscribe(["orders"]);
Dconsumer.subscribe("topic=orders");
Step-by-Step Solution
Solution:
  1. Step 1: Check the subscribe() method signature

    Kafka's subscribe() expects a collection of topic names, such as a List or Set.
  2. Step 2: Validate each option

    consumer.subscribe(Collections.singletonList("orders")); uses Collections.singletonList which is a valid List containing "orders". The other options use incorrect argument types or syntax.
  3. Final Answer:

    consumer.subscribe(Collections.singletonList("orders")); -> Option B
  4. Quick Check:

    subscribe() needs a collection of topics [OK]
Quick Trick: Use a list or set to subscribe to topics [OK]
Common Mistakes:
  • Passing a single string instead of a collection
  • Using array syntax not supported in Java
  • Adding extra text like 'topic=' in topic name

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes