Bird
Raised Fist0

Identify the error in the following code snippet:

medium📝 Debug Q6 of Q15
Kafka - Streams
Identify the error in the following code snippet:
var builder = new StreamsBuilder();
var table = builder.table("products");
table.foreach((k, v) -> System.out.println(k + ":" + v));
AThe topic name "products" is invalid.
B<code>KTable</code> does not have a <code>foreach</code> method directly.
CThe lambda syntax is incorrect for <code>foreach</code>.
DMissing call to <code>toStream()</code> before <code>foreach</code>.
Step-by-Step Solution
Solution:
  1. Step 1: Check KTable API methods

    KTable does not support foreach directly; it must be converted to a stream first.
  2. Step 2: Verify other options

    Topic name is valid, lambda syntax is correct, but foreach is not available on KTable.
  3. Final Answer:

    KTable does not have a foreach method directly. -> Option B
  4. Quick Check:

    KTable foreach method = B [OK]
Quick Trick: Use toStream() before foreach on KTable [OK]
Common Mistakes:
MISTAKES
  • Calling foreach directly on KTable
  • Assuming lambda syntax error
  • Thinking topic name causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes