Challenge - 5 Problems
Safe Call Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Kotlin code using safe call operator?
Consider the following Kotlin code snippet. What will it print when run?
Kotlin
data class Person(val name: String?, val age: Int?) fun main() { val person: Person? = Person(null, 25) println(person?.name ?: "Unknown") }
Attempts:
2 left
💡 Hint
The safe call operator returns null if the object is null, and the Elvis operator provides a default value.
✗ Incorrect
The safe call operator '?.' checks if 'person' is null. Since 'person' is not null but 'name' is null, 'person?.name' evaluates to null. The Elvis operator '?:' then returns "Unknown" as the default.
❓ Predict Output
intermediate2:00remaining
What does this Kotlin code print when using safe call operator with a null object?
Analyze the code below and select the output it produces.
Kotlin
fun main() { val text: String? = null val length = text?.length ?: -1 println(length) }
Attempts:
2 left
💡 Hint
Safe call returns null if the object is null, then Elvis operator provides fallback.
✗ Incorrect
Since 'text' is null, 'text?.length' is null. The Elvis operator returns -1 as fallback, so the output is -1.
❓ Predict Output
advanced2:00remaining
What is the output of this Kotlin code with chained safe calls?
Given the following Kotlin code, what will be printed?
Kotlin
class Address(val city: String?) class User(val address: Address?) fun main() { val user: User? = User(null) println(user?.address?.city ?: "No city") }
Attempts:
2 left
💡 Hint
Safe calls chain returns null if any part is null, then Elvis operator provides default.
✗ Incorrect
The 'user' is not null, but 'address' is null. So 'user?.address?.city' evaluates to null, and the Elvis operator returns "No city".
❓ Predict Output
advanced2:00remaining
What error does this Kotlin code raise when using safe call operator incorrectly?
Examine the code and determine what error it produces when run.
Kotlin
fun main() { val list: List<String>? = null val first = list?.get(0)!! println(first) }
Attempts:
2 left
💡 Hint
The '!!' operator forces a non-null assertion and throws if the value is null.
✗ Incorrect
Since 'list' is null, 'list?.get(0)' is null. The '!!' operator then throws KotlinNullPointerException.
🧠 Conceptual
expert3:00remaining
How many items are in the resulting list after this Kotlin code runs?
Consider this Kotlin code using safe call operator and list filtering. How many items does 'filtered' contain?
Kotlin
fun main() { val list: List<String?> = listOf("apple", null, "banana", null, "cherry") val filtered = list.map { it?.length ?: 0 }.filter { it > 5 } println(filtered.size) }
Attempts:
2 left
💡 Hint
Check the length of each string, replace null with 0, then count how many are greater than 5.
✗ Incorrect
Lengths: "apple"=5, null=0, "banana"=6, null=0, "cherry"=6. Filter >5 keeps 6 and 6, so 2 items.