Bird
Raised Fist0

Given the following Kafka Streams code snippet, what will be the output printed?

medium📝 Predict Output Q13 of Q15
Kafka - Streams
Given the following Kafka Streams code snippet, what will be the output printed?
var stream = builder.stream("orders");
var table = builder.table("orders");
stream.foreach((k, v) -> System.out.println("Stream: " + k + ":" + v));
table.toStream().foreach((k, v) -> System.out.println("Table: " + k + ":" + v));
Assuming the topic orders has these records in order: (1, "A"), (1, "B"), (2, "C").
AStream prints all three records; Table prints only latest for each key: (1, "B") and (2, "C")
BStream prints only latest record per key; Table prints all records
CBoth Stream and Table print all three records
DStream prints no records; Table prints all three records
Step-by-Step Solution
Solution:
  1. Step 1: Understand KStream output

    KStream processes every event as it arrives, so it prints all three records: Stream: 1:A, Stream: 1:B, Stream: 2:C.
  2. Step 2: Understand KTable.toStream() output

    KTable.toStream() emits the latest update per key. It will print updates for key 1 twice (first "A", then "B") and key 2 once ("C"). However, in practice, KTable.toStream() emits only the latest update per key, so the final output for the table stream will be (1, "B") and (2, "C").
  3. Final Answer:

    Stream prints all three records; Table prints only latest for each key: (1, "B") and (2, "C") -> Option A
  4. Quick Check:

    KStream emits all events; KTable.toStream() emits latest per key [OK]
Quick Trick: KStream prints all events; KTable.toStream() prints latest per key [OK]
Common Mistakes:
MISTAKES
  • Thinking KTable.toStream() prints all updates
  • Thinking KStream shows only latest per key
  • Confusing output order between stream and table

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes