Complete the code to define an extension function on a nullable String that returns its length or 0 if null.
fun String?.nullableLength(): Int = this?.[1] ?: 0
The extension function uses the safe call operator ?. to access the length property if the string is not null. If it is null, it returns 0.
Complete the code to safely convert a nullable String to uppercase using an extension function.
fun String?.toUpperCaseSafe(): String = this?.[1]() ?: ""
In modern Kotlin, strings are converted to uppercase using the uppercase() function. The blank is filled with uppercase since the parentheses follow the blank in the template.
Fix the error in the extension function that returns the first character of a nullable String or '-' if null.
fun String?.firstCharOrDash(): Char = this?.[1] ?: '-'
first() is a standard library function on CharSequence (including String) that returns the first Char. Since the template lacks parentheses after the blank, include them in the blank: first().
Fill both blanks to create an extension function that returns the last character of a nullable String or '?' if null or empty.
fun String?.lastCharOrQuestion(): Char = if (this != null && this.[1] > 0) this[[2]] else '?'
size instead of length for Stringlast() without proper null/empty handlingThe function checks if the string is not null and has length greater than zero. Then it returns the last character using Kotlin's indexing syntax [length - 1].
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.
fun String?.charCountMap(): Map<Char, Int> = this?.[1]()?.[2] { it -> it }?.eachCount() ?: [3]()
toList() on String directly (works but not optimal; toCharArray() is fine)eachCount() after groupingBynull or wrong empty collection instead of emptyMap()Convert the string to a CharArray with toCharArray(), group by character identity with groupingBy { it }, call eachCount() to get Map<Char, Int>, or return emptyMap() if null.