Bird
0
0

What will the following Kotlin code print?

easy📝 Conceptual Q2 of 15
Kotlin - Control Flow as Expressions
What will the following Kotlin code print?
val score = 85
val grade = when (score) {
  in 90..100 -> "A"
  in 80..89 -> "B"
  else -> "C"
}
println(grade)
AA
BError
CC
DB
Step-by-Step Solution
Solution:
  1. Step 1: Check the value of score and matching range

    Score is 85, which falls in the range 80..89.
  2. Step 2: Determine the matched branch in when

    The branch in 80..89 returns "B".
  3. Final Answer:

    B -> Option D
  4. Quick Check:

    Range matching in when = B [OK]
Quick Trick: Use ranges in when for concise conditions [OK]
Common Mistakes:
MISTAKES
  • Confusing range boundaries
  • Forgetting else branch
  • Using incorrect range syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes