Bird
0
0

What will be the output of the following Kotlin code?

medium📝 Predict Output Q13 of 15
Kotlin - Control Flow as Expressions
What will be the output of the following Kotlin code?
val x = 3
when (x) {
  1, 2 -> println("Small")
  in 3..5 -> println("Medium")
  else -> println("Large")
}
AMedium
BSmall
CLarge
DNo output (runtime error)
Step-by-Step Solution
Solution:
  1. Step 1: Check the value of x

    The variable x is 3.
  2. Step 2: Match x with when cases

    3 is not in 1 or 2, but it is in the range 3..5, so "Medium" is printed.
  3. Final Answer:

    Medium -> Option A
  4. Quick Check:

    3 in 3..5 means output is "Medium" [OK]
Quick Trick: Check ranges and multiple values carefully [OK]
Common Mistakes:
MISTAKES
  • Ignoring ranges and picking wrong case
  • Assuming 3 matches 1 or 2
  • Thinking else runs when a range matches

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes