Bird
0
0

Which of the following is the correct syntax to check if a variable num is either 1, 2, or 3 using when with multiple conditions?

easy📝 Syntax Q12 of 15
Kotlin - Control Flow as Expressions

Which of the following is the correct syntax to check if a variable num is either 1, 2, or 3 using when with multiple conditions?

when (num) {
    ? -> println("Number is 1, 2, or 3")
    else -> println("Other number")
}
A1, 2, 3
B1 || 2 || 3
C1 && 2 && 3
D[1, 2, 3]
Step-by-Step Solution
Solution:
  1. Step 1: Recall Kotlin when syntax for multiple conditions

    In Kotlin, multiple conditions in a when case are separated by commas without operators.
  2. Step 2: Match the correct syntax

    1, 2, 3 uses commas to separate values: 1, 2, 3, which is correct. Other options use logical operators or list syntax, which are invalid here.
  3. Final Answer:

    1, 2, 3 -> Option A
  4. Quick Check:

    Multiple conditions use commas [OK]
Quick Trick: Use commas, not operators, to separate conditions [OK]
Common Mistakes:
MISTAKES
  • Using || or && inside when conditions
  • Trying to use list syntax like [1, 2, 3]
  • Missing commas between values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes