0
0
Kotlinprogramming~10 mins

When without argument in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print the correct message based on the number using a when expression without argument.

Kotlin
val number = 2
when {
    number == 1 -> println("One")
    number == [1] -> println("Two")
    else -> println("Other")
}
Drag options to blanks, or click blank then click option'
A2
B3
C0
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Using a value other than 2 in the condition.
Trying to use when with argument syntax here.
2fill in blank
medium

Complete the code to print the correct message for the character using when without argument.

Kotlin
val ch = 'a'
when {
    ch.isUpperCase() -> println("Uppercase")
    ch == [1] -> println("Lowercase a")
    else -> println("Other")
}
Drag options to blanks, or click blank then click option'
A'a'
B'b'
C'z'
D'A'
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase 'A' instead of lowercase 'a'.
Using double quotes instead of single quotes.
3fill in blank
hard

Fix the error in the when expression without argument to correctly check if the number is positive.

Kotlin
val num = -5
when {
    num [1] 0 -> println("Positive")
    num == 0 -> println("Zero")
    else -> println("Negative")
}
Drag options to blanks, or click blank then click option'
A==
B>
C<
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of >.
Using equality operator == incorrectly.
4fill in blank
hard

Fill both blanks to create a when expression without argument that prints if a number is even and positive.

Kotlin
val x = 4
when {
    x [1] 0 && x [2] 2 == 0 -> println("Even and positive")
    else -> println("Other")
}
Drag options to blanks, or click blank then click option'
A>
B==
C%
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > for positivity.
Using == instead of % for even check.
5fill in blank
hard

Fill both blanks to create a when expression without argument that prints if a string is empty, blank, or has content.

Kotlin
val text = "  "
when {
    text.[1]() -> println("Empty")
    text.[2]() -> println("Blank")
    else -> println("Has content")
}
Drag options to blanks, or click blank then click option'
AisEmpty
BisBlank
Clength
DisEmptyOrBlank
Attempts:
3 left
💡 Hint
Common Mistakes
Using length() as a function instead of property.
Confusing isEmpty() and isBlank().