Bird
0
0

Find the error in this Kotlin code snippet:

medium📝 Debug Q7 of 15
Kotlin - Control Flow as Expressions
Find the error in this Kotlin code snippet:
val y = 7
val output = when {
  y == 5 -> "Five"
  y == 7 -> "Seven"
  else "Unknown"
}
AVariable y is not declared
BMissing parentheses after when
CUsing semicolons instead of arrows
DMissing arrow (->) in else branch
Step-by-Step Solution
Solution:
  1. Step 1: Check syntax of when branches

    Each branch must have an arrow (->) before the value; else branch is missing it.
  2. Step 2: Confirm other syntax parts

    Parentheses are not needed when using conditions; variable y is declared.
  3. Final Answer:

    Missing arrow (->) in else branch -> Option D
  4. Quick Check:

    All when branches need arrows [OK]
Quick Trick: Every when branch needs an arrow (->) before value [OK]
Common Mistakes:
MISTAKES
  • Omitting arrow in else branch
  • Adding parentheses unnecessarily
  • Using colons instead of arrows

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes