Back to questions
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
fun main() {
val a: String? = null
val b: String? = "hi"
println(a?.length ?: -1)
println(b?.length ?: -1)
}-1 2
- Expecting
null 2: forgetting the?:.a?.lengthisnull, and?: -1replaces thatnullwith-1before it is printed. - Expecting
-1 -1: thinking?:always gives its right side. It does so only when the left side isnull;b?.lengthis2, so2is printed.
- What does
println(a?.length)print without the?:? - What does
println("12x".toIntOrNull() ?: 0)print?