Bird
0
0

In Kotlin, what will the when expression return if the variable num falls within the range 5..15?

easy📝 Conceptual Q1 of 15
Kotlin - Control Flow as Expressions
In Kotlin, what will the when expression return if the variable num falls within the range 5..15?
AAn error because ranges cannot be used in <code>when</code>
BThe value associated with the <code>else</code> branch
CThe value associated with the <code>in 5..15</code> branch
DNull
Step-by-Step Solution
Solution:
  1. Step 1: Understand when with ranges

    The when expression checks if the value matches any condition, including ranges.
  2. Step 2: Check if num is in the range

    If num is within 5..15, the corresponding branch executes.
  3. Final Answer:

    The value associated with the in 5..15 branch -> Option C
  4. Quick Check:

    Range matches trigger that branch [OK]
Quick Trick: Ranges in when match the branch directly [OK]
Common Mistakes:
MISTAKES
  • Assuming else branch runs even if range matches
  • Thinking ranges can't be used in when
  • Confusing range boundaries

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes