Bird
0
0

Given this serializer code snippet, what will serialize() return for input "hello"?

medium📝 Predict Output Q13 of 15
Kafka - Advanced Stream Processing

Given this serializer code snippet, what will serialize() return for input "hello"?

public byte[] serialize(String topic, String data) {
    return data == null ? null : data.getBytes(StandardCharsets.UTF_8);
}
A[104, 101, 108, 108, 111]
Bnull
C["h", "e", "l", "l", "o"]
DThrows NullPointerException
Step-by-Step Solution
Solution:
  1. Step 1: Understand serialize method

    If data is not null, it returns UTF-8 bytes of the string.
  2. Step 2: Convert "hello" to bytes

    "hello" in UTF-8 bytes is [104, 101, 108, 108, 111].
  3. Final Answer:

    [104, 101, 108, 108, 111] -> Option A
  4. Quick Check:

    "hello" bytes = [104,101,108,108,111] [OK]
Quick Trick: String.getBytes(UTF-8) returns byte array [OK]
Common Mistakes:
  • Thinking it returns characters array
  • Assuming null is returned for non-null input
  • Expecting exception on valid input

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes