Bird
0
0

What is the output of this Kotlin code?

medium📝 Predict Output Q4 of 15
Kotlin - Control Flow as Expressions
What is the output of this Kotlin code?
val x = "hello"
val result = when (x.length) {
  3 -> "Three"
  5 -> "Five"
  else -> "Other"
}
println(result)
AThree
BFive
COther
DError
Step-by-Step Solution
Solution:
  1. Step 1: Calculate length of string x

    "hello" has length 5.
  2. Step 2: Match length with when branches

    Length 5 matches the second branch returning "Five".
  3. Final Answer:

    Five -> Option B
  4. Quick Check:

    Matching integer in when = Five [OK]
Quick Trick: Use when with expressions like string length [OK]
Common Mistakes:
MISTAKES
  • Miscounting string length
  • Confusing branch values
  • Forgetting else branch

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes