Challenge - 5 Problems
Kotlin Variables Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate2:00remaining
Understanding val and var declaration
What will be the output of this Kotlin code snippet?
val x = 10
var y = 5
y = 7
println("x = $x, y = $y")Android Kotlin
val x = 10 var y = 5 y = 7 println("x = $x, y = $y")
Attempts:
2 left
💡 Hint
Remember val means constant, var means variable.
✗ Incorrect
val declares a read-only variable, so x cannot be changed. var declares a mutable variable, so y can be reassigned.
❓ ui_behavior
intermediate2:00remaining
Null safety with nullable types
What will happen when this Kotlin code runs?
var name: String? = null println(name?.length)
Android Kotlin
var name: String? = null println(name?.length)
Attempts:
2 left
💡 Hint
The ?. operator safely accesses properties of nullable variables.
✗ Incorrect
The safe call operator ?. returns null if the variable is null, so name?.length prints null safely.
❓ lifecycle
advanced2:00remaining
Lateinit var usage and initialization
What error will this Kotlin code cause when run?
lateinit var message: String println(message)
Android Kotlin
lateinit var message: String println(message)
Attempts:
2 left
💡 Hint
Lateinit variables must be initialized before use.
✗ Incorrect
Accessing a lateinit var before initialization throws UninitializedPropertyAccessException at runtime.
🔧 Debug
advanced2:00remaining
Null safety and !! operator behavior
What happens when this Kotlin code runs?
var text: String? = null println(text!!.length)
Android Kotlin
var text: String? = null println(text!!.length)
Attempts:
2 left
💡 Hint
The !! operator asserts non-null and throws if null.
✗ Incorrect
Using !! on a null variable throws KotlinNullPointerException at runtime.
🧠 Conceptual
expert3:00remaining
Choosing val or var for thread safety
In a multi-threaded Android app, which variable declaration is safer to avoid unexpected changes?
val data = mutableListOf() var data = mutableListOf ()
Attempts:
2 left
💡 Hint
val prevents changing the reference but does not make the object immutable.
✗ Incorrect
val prevents the variable from pointing to a different list, but the list itself can still be modified, so thread safety depends on synchronization, not val/var alone.