Bird
0
0

What is the output of the following Kotlin code?

medium📝 Predict Output Q13 of 15
Kotlin - Control Flow as Expressions
What is the output of the following Kotlin code?
val x = 5
val result = when {
  x < 0 -> "Negative"
  x == 0 -> "Zero"
  x in 1..10 -> "Small"
  else -> "Large"
}
println(result)
ASmall
BZero
CNegative
DLarge
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate conditions in order

    x = 5 is not less than 0, not equal to 0, but is in 1..10 range, so the third case matches.
  2. Step 2: Determine returned value

    The matched case returns "Small", so result is "Small".
  3. Final Answer:

    Small -> Option A
  4. Quick Check:

    5 in 1..10 = true, returns "Small" [OK]
Quick Trick: Check conditions top to bottom; first true case returns value [OK]
Common Mistakes:
MISTAKES
  • Assuming else branch runs for 5
  • Confusing equality and range checks
  • Ignoring order of conditions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes