Bird
0
0

How can you use a Kotlin when expression to differentiate between Int values inside the range 1..10, Double values, and all other types, returning a different message for each?

hard📝 Application Q9 of 15
Kotlin - Control Flow as Expressions
How can you use a Kotlin when expression to differentiate between Int values inside the range 1..10, Double values, and all other types, returning a different message for each?
AUse <code>is Int</code> with <code>in 1..10</code>, <code>is Double</code>, and <code>else</code> branches.
BUse <code>in 1..10</code> for all types, <code>is Double</code>, and <code>else</code>.
CUse <code>is Int in 1..10</code>, <code>is Double</code>, and <code>else</code>.
DUse <code>is Int</code>, <code>is Double in 1..10</code>, and <code>else</code>.
Step-by-Step Solution
Solution:
  1. Step 1: Combine type and range checks properly

    Check if value is Int and in 1..10 using nested conditions inside when.
  2. Step 2: Use separate branches for Double and else

    Use is Double branch and else for all other cases.
  3. Final Answer:

    Use is Int with in 1..10, is Double, and else branches. -> Option A
  4. Quick Check:

    Combine type and range checks correctly in when = A [OK]
Quick Trick: Check type first, then range inside when branch [OK]
Common Mistakes:
MISTAKES
  • Trying to combine 'is Int in 1..10' which is invalid
  • Using range check without type check
  • Misplacing range check on Double type

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes