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: Any = 7
val result = when (x) {
  in 1..5 -> "Low"
  in 6..10 -> "Medium"
  else -> "High"
}
println(result)
ALow
BCompilation error
CHigh
DMedium
Step-by-Step Solution
Solution:
  1. Step 1: Check the value of x and matching ranges

    x is 7, which falls in the range 6..10.
  2. Step 2: Determine which branch executes

    The branch in 6..10 -> "Medium" matches, so "Medium" is returned.
  3. Final Answer:

    Medium -> Option D
  4. Quick Check:

    Value 7 in 6..10 returns 'Medium' = D [OK]
Quick Trick: Match value to correct range branch in when [OK]
Common Mistakes:
MISTAKES
  • Choosing 'Low' because 7 > 5
  • Assuming else branch runs for 7
  • Thinking code causes compilation error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes