Bird
0
0

What will this Kotlin code print?

medium📝 Predict Output Q5 of 15
Kotlin - Control Flow as Expressions
What will this Kotlin code print?
val num = 7
val output = when {
  num % 2 == 0 -> "Even"
  num % 2 != 0 -> "Odd"
  else -> "Unknown"
}
println(output)
AUnknown
BEven
COdd
DError
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate conditions in when without argument

    num is 7, so num % 2 == 0 is false, num % 2 != 0 is true.
  2. Step 2: The first true condition branch executes

    The second branch matches and returns "Odd".
  3. Final Answer:

    Odd -> Option C
  4. Quick Check:

    when without argument picks first true branch = Odd [OK]
Quick Trick: Use when without argument for boolean checks [OK]
Common Mistakes:
MISTAKES
  • Confusing modulo results
  • Expecting else branch to run first
  • Syntax errors in when without argument

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes