Complete the code to declare a nullable variable in Kotlin.
var name: String[1] = nullIn Kotlin, adding ? after a type makes it nullable, allowing it to hold null.
Complete the code to safely access the length of a nullable string.
val length = name[1]lengthThe safe call operator ?. allows accessing a property only if the object is not null.
Fix the error in the code to throw an exception if the nullable variable is null.
val nonNullName = name[1]The !! operator asserts that the value is not null and throws an exception if it is.
Fill both blanks to provide a default value if the nullable variable is null.
val displayName = name [1] "Unknown"
The Elvis operator ?: returns the left value if not null, otherwise the right value.
Fill all three blanks to filter a list of nullable strings and print their lengths safely.
val names: List<String?> = listOf("Anna", null, "Bob") val lengths = names.filterNotNull().map { it[1]length } lengths.forEach { println(it[2]toString()[3]) }
After filtering nulls, it.length accesses length safely. Then println(it + "") prints the length as string.