0
0
Kotlinprogramming~10 mins

Why expressions over statements matters in Kotlin - Test Your Understanding

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

Complete the code to assign the maximum of two numbers to max using an expression.

Kotlin
val max = if (a > b) [1] else b
Drag options to blanks, or click blank then click option'
Aa
B0
Ca + b
Db
Attempts:
3 left
💡 Hint
Common Mistakes
Using if as a statement without returning a value.
Assigning a fixed value like 0 instead of a.
2fill in blank
medium

Complete the code to assign the result of the expression that returns the length of the string or 0 if the string is null.

Kotlin
val length = str?.length ?: [1]
Drag options to blanks, or click blank then click option'
Anull
Bstr.length
C-1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using null as default which causes type mismatch.
Using str.length which can cause a null pointer exception.
3fill in blank
hard

Fix the error in the expression that tries to assign the result of a when expression to a variable.

Kotlin
val result = when (x) {
    1 -> "One"
    2 -> "Two"
    else -> [1]
}
Drag options to blanks, or click blank then click option'
Anull
B"Other"
Cprint("Other")
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using print("Other") which returns Unit, not String.
Returning null which may cause type mismatch.
4fill in blank
hard

Fill both blanks to create a map of words to their lengths, including only words longer than 3 characters.

Kotlin
val lengths = words.filter { word -> word.length > 3 }.map { word -> [1] to [2] }.toMap()
Drag options to blanks, or click blank then click option'
Aword
Bword.length
Cword.uppercase()
Dword.count()
Attempts:
3 left
💡 Hint
Common Mistakes
Using word.uppercase() as key which changes the word.
Using word.count() which is not a Kotlin String method.
5fill in blank
hard

Fill all three blanks to create a map of uppercase words to their lengths, including only words longer than 4 characters.

Kotlin
val result = words.filter { word -> word.length [3] 4 }.map { word -> [1] to [2] }.toMap()
Drag options to blanks, or click blank then click option'
Aword.uppercase()
Bword.length
C>
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using word instead of uppercase for keys.
Using < instead of > for filtering.