Bird
0
0

Which of the following is a valid use of when expression without an argument in Kotlin?

easy📝 Conceptual Q1 of 15
Kotlin - Control Flow as Expressions
Which of the following is a valid use of when expression without an argument in Kotlin?
Awhen x { > 10 -> println("Greater") else -> println("Smaller") }
Bwhen { x > 10 -> println("Greater") else -> println("Smaller") }
Cwhen (x) { x > 10 -> println("Greater") else -> println("Smaller") }
Dwhen { x == 10, x == 20 -> println("Equal") }
Step-by-Step Solution
Solution:
  1. Step 1: Understand when without argument syntax

    When expression without argument uses boolean conditions directly in branches.
  2. Step 2: Check each option for correct syntax

    when { x > 10 -> println("Greater") else -> println("Smaller") } correctly uses when without argument and boolean conditions. when (x) { x > 10 -> println("Greater") else -> println("Smaller") } incorrectly uses argument with conditions. when x { > 10 -> println("Greater") else -> println("Smaller") } has invalid syntax. when { x == 10, x == 20 -> println("Equal") } has invalid syntax (comma-separated values not allowed without subject).
  3. Final Answer:

    when { x > 10 -> println("Greater") else -> println("Smaller") } -> Option B
  4. Quick Check:

    when without argument = when { x > 10 -> println("Greater") else -> println("Smaller") } [OK]
Quick Trick: Use when without argument for boolean conditions [OK]
Common Mistakes:
MISTAKES
  • Using conditions with when argument incorrectly
  • Omitting else branch in when without argument
  • Wrong syntax like missing braces

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes