Bird
0
0

Which of the following when branches correctly checks if a variable score is either 90, 95, or 100?

easy📝 Conceptual Q2 of 15
Kotlin - Control Flow as Expressions
Which of the following when branches correctly checks if a variable score is either 90, 95, or 100?
Awhen(score) { 90 || 95 || 100 -> println("Excellent") }
Bwhen(score) { 90, 95, 100 -> println("Excellent") }
Cwhen(score) { in 90..100 -> println("Excellent") }
Dwhen(score) { 90 && 95 && 100 -> println("Excellent") }
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct syntax for multiple values in when

    Kotlin uses commas to separate multiple values in a single when branch, so 90, 95, 100 is correct.
  2. Step 2: Check other options for syntax errors or incorrect logic

    Logical operators like && or || are not valid in when conditions; 'in 90..100' checks a range, not specific values.
  3. Final Answer:

    when(score) { 90, 95, 100 -> println("Excellent") } -> Option B
  4. Quick Check:

    Use commas for multiple values in when = D [OK]
Quick Trick: Use commas, not && or ||, for multiple when conditions [OK]
Common Mistakes:
MISTAKES
  • Using logical operators instead of commas
  • Confusing range with specific values
  • Syntax errors from invalid operators

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes