Bird
0
0

What will be printed by the following Kotlin code?

medium📝 Predict Output Q5 of 15
Kotlin - Control Flow as Expressions
What will be printed by the following Kotlin code?
val temperature = 15
val description = when {
  temperature > 30 -> "Hot"
  temperature in 15..30 -> "Warm"
  temperature in 0..14 -> "Cold"
  else -> "Freezing"
}
println(description)
AHot
BWarm
CCold
DFreezing
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate conditions in order

    temperature is 15, check each condition:
  2. Step 2: Check ranges

    15 is not > 30, but is in 15..30 range, so "Warm" matches.
  3. Final Answer:

    Warm -> Option B
  4. Quick Check:

    Check which range 15 falls into [OK]
Quick Trick: Check conditions top to bottom, first match returns value [OK]
Common Mistakes:
MISTAKES
  • Assuming 15 is 'Cold' instead of 'Warm'
  • Ignoring range checks order
  • Missing else branch causing errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes