Complete the code to print the correct message based on the number using a when expression without argument.
val number = 2 when { number == 1 -> println("One") number == [1] -> println("Two") else -> println("Other") }
The when expression without argument uses conditions. Here, number == 2 matches the second case.
Complete the code to print the correct message for the character using when without argument.
val ch = 'a' when { ch.isUpperCase() -> println("Uppercase") ch == [1] -> println("Lowercase a") else -> println("Other") }
The condition checks if ch is equal to lowercase 'a'.
Fix the error in the when expression without argument to correctly check if the number is positive.
val num = -5 when { num [1] 0 -> println("Positive") num == 0 -> println("Zero") else -> println("Negative") }
< instead of >.== incorrectly.The condition num > 0 checks if the number is positive.
Fill both blanks to create a when expression without argument that prints if a number is even and positive.
val x = 4 when { x [1] 0 && x [2] 2 == 0 -> println("Even and positive") else -> println("Other") }
< instead of > for positivity.== instead of % for even check.The first condition checks if x > 0 (positive), and the second uses x % 2 == 0 to check evenness.
Fill both blanks to create a when expression without argument that prints if a string is empty, blank, or has content.
val text = " " when { text.[1]() -> println("Empty") text.[2]() -> println("Blank") else -> println("Has content") }
length() as a function instead of property.isEmpty() and isBlank().isEmpty() checks if the string has zero length, isBlank() checks if it contains only whitespace, and length is not a function but a property, so it is a distractor here.