Bird
0
0

You want to print a message based on temperature using when without argument. Which code correctly prints "Cold" if temp < 10, "Warm" if between 10 and 25, and "Hot" otherwise?

hard📝 Application Q8 of 15
Kotlin - Control Flow as Expressions
You want to print a message based on temperature using when without argument. Which code correctly prints "Cold" if temp < 10, "Warm" if between 10 and 25, and "Hot" otherwise?
Awhen { temp < 10 -> println("Cold") temp in 10..25 -> println("Warm") else -> println("Hot") }
Bwhen (temp) { < 10 -> println("Cold") in 10..25 -> println("Warm") else -> println("Hot") }
Cwhen { temp < 10: println("Cold") temp in 10..25: println("Warm") else: println("Hot") }
Dwhen { temp < 10 -> println("Cold") temp > 10 && temp < 25 -> println("Warm") else -> println("Hot") }
Step-by-Step Solution
Solution:
  1. Step 1: Check syntax correctness

    when { temp < 10 -> println("Cold") temp in 10..25 -> println("Warm") else -> println("Hot") } uses correct when without argument syntax with -> and else branch.
  2. Step 2: Verify condition coverage

    when { temp < 10 -> println("Cold") temp in 10..25 -> println("Warm") else -> println("Hot") } covers temp < 10, temp in 10..25, else for others correctly.
  3. Final Answer:

    when { temp < 10 -> println("Cold") temp in 10..25 -> println("Warm") else -> println("Hot") } -> Option A
  4. Quick Check:

    Use when without argument with proper arrows and else [OK]
Quick Trick: Use range with in and else for full coverage [OK]
Common Mistakes:
MISTAKES
  • Using parentheses with when without argument
  • Using colon instead of arrow
  • Incorrect condition ranges

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes