Bird
0
0

How does Kotlin's when expression handle multiple values listed in a single branch separated by commas?

easy📝 Conceptual Q1 of 15
Kotlin - Control Flow as Expressions

How does Kotlin's when expression handle multiple values listed in a single branch separated by commas?

AIt treats the values as a range to check against the subject.
BIt requires all values to match simultaneously.
CIt matches if the subject equals any one of the listed values.
DIt throws a syntax error because multiple values are not allowed.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the syntax

    In Kotlin, multiple conditions in a when branch separated by commas mean the branch matches if the subject equals any one of those values.
  2. Step 2: Behavior of multiple conditions

    The when expression checks each branch in order and executes the first branch where the subject matches any of the comma-separated values.
  3. Final Answer:

    It matches if the subject equals any one of the listed values. -> Option C
  4. Quick Check:

    Multiple values separated by commas act like an OR condition. [OK]
Quick Trick: Commas mean OR condition in when branches [OK]
Common Mistakes:
MISTAKES
  • Thinking all values must match simultaneously
  • Confusing commas with logical AND
  • Assuming syntax error with multiple values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes