0
0
Kotlinprogramming~20 mins

Elvis operator deep usage in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Elvis Operator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Elvis operator with nested nullables
What is the output of this Kotlin code snippet?
Kotlin
val a: String? = null
val b: String? = "Hello"
val c: String? = null

val result = a ?: b ?: c ?: "Default"
println(result)
Anull
BDefault
CHello
DCompilation error
Attempts:
2 left
💡 Hint
The Elvis operator returns the first non-null value from left to right.
Predict Output
intermediate
2:00remaining
Elvis operator with function calls
What will be printed by this Kotlin code?
Kotlin
fun getName(): String? = null
fun getDefaultName(): String = "Guest"

val name = getName() ?: getDefaultName()
println(name)
Anull
BGuest
CCompilation error
DgetDefaultName
Attempts:
2 left
💡 Hint
Elvis operator calls the right side only if the left side is null.
Predict Output
advanced
2:00remaining
Elvis operator with smart casting and let
What is the output of this Kotlin code?
Kotlin
val input: String? = "Kotlin"

val length = input?.let { it.length } ?: -1
println(length)
A6
B-1
Cnull
DCompilation error
Attempts:
2 left
💡 Hint
The safe call with let executes only if input is not null.
Predict Output
advanced
2:00remaining
Elvis operator with multiple nullable types and side effects
What will be printed by this Kotlin code?
Kotlin
var count = 0

fun getFirst(): String? {
    count++
    return null
}

fun getSecond(): String? {
    count++
    return "Second"
}

val result = getFirst() ?: getSecond() ?: "Default"
println("$result $count")
ADefault 1
BSecond 1
CDefault 2
DSecond 2
Attempts:
2 left
💡 Hint
Elvis operator evaluates left to right and stops at first non-null.
Predict Output
expert
3:00remaining
Complex Elvis operator with nested expressions and nullables
What is the output of this Kotlin code?
Kotlin
data class User(val name: String?, val nickname: String?)

fun getUser(): User? = User(null, "Nick")

val displayName = getUser()?.name ?: getUser()?.nickname ?: "Unknown"
println(displayName)
ANick
BUnknown
CCompilation error
Dnull
Attempts:
2 left
💡 Hint
Elvis operator chains check for non-null values from left to right.