Bird
0
0

Which of the following is the correct syntax to check if a variable day is either "Mon", "Tue", or "Wed" using when?

easy📝 Syntax Q3 of 15
Kotlin - Control Flow as Expressions
Which of the following is the correct syntax to check if a variable day is either "Mon", "Tue", or "Wed" using when?
Awhen(day) { "Mon", "Tue", "Wed" -> println("Start of week") }
Bwhen(day) { "Mon" || "Tue" || "Wed" -> println("Start of week") }
Cwhen(day) { listOf("Mon", "Tue", "Wed") -> println("Start of week") }
Dwhen(day) { "Mon" && "Tue" && "Wed" -> println("Start of week") }
Step-by-Step Solution
Solution:
  1. Step 1: Recall correct syntax for multiple conditions in when

    Multiple values are separated by commas inside the when branch.
  2. Step 2: Evaluate other options for syntax correctness

    Logical operators like || or && are invalid; listOf("Mon", "Tue", "Wed") checks if day equals the list object, not membership.
  3. Final Answer:

    when(day) { "Mon", "Tue", "Wed" -> println("Start of week") } -> Option A
  4. Quick Check:

    Use commas for multiple string conditions in when = B [OK]
Quick Trick: Separate multiple string conditions with commas in when [OK]
Common Mistakes:
MISTAKES
  • Using logical operators instead of commas
  • Trying to use listOf without 'in' keyword
  • Syntax errors from invalid operators

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes