Complete the code to declare a list that cannot hold null values.
val numbers: List<Int> = listOf(1, 2, 3, [1])
The list is declared to hold only non-null Int values, so adding null is not allowed.
Complete the code to declare a list that can hold null values.
val nullableNumbers: List<[1]> = listOf(1, null, 3)
Int which disallows nullsStringUsing Int? allows the list to hold null values along with integers.
Fix the error in the code to safely access the first element which might be null.
val first: Int? = nullableNumbers.[1](0)
get which throws an exception if index is invalidfirst which throws exception if list is emptygetOrNull safely returns the element at the index or null if out of bounds, preventing exceptions.
Fill both blanks to create a map that holds nullable values and safely access a value.
val map: Map<String, [1]> = mapOf("a" to 1, "b" to null) val value: [2]? = map["b"]
Int when nulls are presentThe map holds nullable integers (Int?), and accessing a key returns a nullable Int value.
Fill all three blanks to create a list of nullable strings and filter out nulls safely.
val items: List<[1]> = listOf("apple", null, "banana") val filtered: List<[2]> = items.filterNotNull() val firstItem: [3] = filtered[0]
The original list holds nullable strings (String?), filtering removes nulls resulting in a list of non-null String, so the first item is a non-null String.