0
0
Kotlinprogramming~10 mins

Nullable receiver extensions 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 define an extension function on a nullable String that returns its length or 0 if null.

Kotlin
fun String?.nullableLength(): Int = this?.[1] ?: 0
Drag options to blanks, or click blank then click option'
Alength
Bsize
Ccount
Dlength()
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses like length() which is not a function
Using size which is not a property of String
Not using the safe call operator
2fill in blank
medium

Complete the code to safely convert a nullable String to uppercase using an extension function.

Kotlin
fun String?.toUpperCaseSafe(): String = this?.[1]() ?: ""
Drag options to blanks, or click blank then click option'
AtoUpper
BtoUpperCase
Cuppercase
Duppercase()
Attempts:
3 left
💡 Hint
Common Mistakes
Using deprecated toUpperCase() instead of uppercase()
Adding parentheses inside the blank like uppercase()
Using incorrect function names like toUpper
3fill in blank
hard

Fix the error in the extension function that returns the first character of a nullable String or '-' if null.

Kotlin
fun String?.firstCharOrDash(): Char = this?.[1] ?: '-'
Drag options to blanks, or click blank then click option'
Afirst()
Bget(0)()
Cget(0)
Dfirst
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting parentheses (e.g., 'first') causing syntax error
Using malformed 'get(0)' or 'get(0)()'
Confusing with non-existent 'first' property
4fill in blank
hard

Fill both blanks to create an extension function that returns the last character of a nullable String or '?' if null or empty.

Kotlin
fun String?.lastCharOrQuestion(): Char = if (this != null && this.[1] > 0) this[[2]] else '?'
Drag options to blanks, or click blank then click option'
Alength
Blast()
Clength - 1
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using size instead of length for String
Using last() without proper null/empty handling
Forgetting to subtract 1 from length for the last index
5fill in blank
hard

Fill all three blanks to create an extension function that returns a map of characters to their counts for a nullable String, or an empty map if null.

Kotlin
fun String?.charCountMap(): Map<Char, Int> = this?.[1]()?.[2] { it -> it }?.eachCount() ?: [3]()
Drag options to blanks, or click blank then click option'
AtoList
BgroupingBy
CemptyMap
DtoCharArray
Attempts:
3 left
💡 Hint
Common Mistakes
Using toList() on String directly (works but not optimal; toCharArray() is fine)
Forgetting eachCount() after groupingBy
Returning null or wrong empty collection instead of emptyMap()