0
0
Kotlinprogramming~20 mins

Nullable types with ? suffix in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kotlin Nullable Types Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Kotlin code with nullable types?
Consider the following Kotlin code snippet. What will it print when run?
Kotlin
fun main() {
    val name: String? = null
    println(name?.length ?: "No name")
}
ANo name
B0
Cnull
DThrows NullPointerException
Attempts:
2 left
💡 Hint
Look at the safe call operator ?. and the Elvis operator ?:
🧠 Conceptual
intermediate
1:30remaining
What does the ? suffix mean in Kotlin type declarations?
In Kotlin, what does adding a question mark (?) after a type mean?
AThe variable can hold only non-null values of that type
BThe variable can hold null or values of that type
CThe variable is immutable
DThe variable is a constant
Attempts:
2 left
💡 Hint
Think about null safety and how Kotlin handles null values.
🔧 Debug
advanced
2:00remaining
What error does this Kotlin code raise?
What error will this Kotlin code produce when compiled or run?
Kotlin
fun main() {
    val text: String = null
    println(text.length)
}
ACompilation error: Null can not be a value of a non-null type String
BNullPointerException at runtime
CCompilation error: Unresolved reference 'length'
DPrints 0
Attempts:
2 left
💡 Hint
Check the type declaration and the assigned value.
Predict Output
advanced
2:00remaining
What is the output of this Kotlin code using safe call and let?
What will this Kotlin program print?
Kotlin
fun main() {
    val number: Int? = 10
    number?.let {
        println(it * 2)
    } ?: println("No number")
}
AThrows NullPointerException
BNo number
Cnull
D20
Attempts:
2 left
💡 Hint
The safe call with let runs the block only if the value is not null.
🧠 Conceptual
expert
2:30remaining
How many items are in the list after filtering nulls with ?. operator?
Given this Kotlin code, how many items does the list nonNullNames contain?
Kotlin
fun main() {
    val names: List<String?> = listOf("Anna", null, "Bob", null, "Cara")
    val nonNullNames = names.mapNotNull { it?.uppercase() }
    println(nonNullNames.size)
}
A0
B2
C3
D5
Attempts:
2 left
💡 Hint
mapNotNull removes null results after applying the lambda.