Bird
0
0

How can you use when without argument to check if a string is empty, blank, or has content, and print appropriate messages?

hard📝 Application Q9 of 15
Kotlin - Control Flow as Expressions
How can you use when without argument to check if a string is empty, blank, or has content, and print appropriate messages?
Awhen { str.isBlank() -> println("Blank") str.isEmpty() -> println("Empty") else -> println("Has content") }
Bwhen { str.isEmpty() -> println("Empty") str.isBlank() -> println("Blank") else -> println("Has content") }
Cwhen (str) { isEmpty() -> println("Empty") isBlank() -> println("Blank") else -> println("Has content") }
Dwhen { str.isEmpty() -> println("Empty") else if (str.isBlank()) -> println("Blank") else -> println("Has content") }
Step-by-Step Solution
Solution:
  1. Step 1: Confirm correct when without argument syntax

    when { str.isEmpty() -> println("Empty") str.isBlank() -> println("Blank") else -> println("Has content") } uses proper conditions with -> and else branch.
  2. Step 2: Check logical order

    Empty string is also blank, so checking isEmpty() first is correct to distinguish.
  3. Final Answer:

    when { str.isEmpty() -> println("Empty") str.isBlank() -> println("Blank") else -> println("Has content") } -> Option B
  4. Quick Check:

    Use when without argument with boolean checks and else [OK]
Quick Trick: Check specific conditions before general ones in when [OK]
Common Mistakes:
MISTAKES
  • Using when with argument and method calls
  • Wrong order causing logic errors
  • Using else if inside when block

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes