0
0
Android Kotlinmobile~20 mins

Variables (val, var) and null safety in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kotlin Variables Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
2: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")
Ax = 10, y = 7
Bx = 10, y = 5
Cx = 7, y = 7
DCompilation error
Attempts:
2 left
💡 Hint
Remember val means constant, var means variable.
ui_behavior
intermediate
2: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)
AThrows NullPointerException
BPrints 0
CCompilation error
DPrints null
Attempts:
2 left
💡 Hint
The ?. operator safely accesses properties of nullable variables.
lifecycle
advanced
2: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)
APrints null
BPrints empty string
CThrows UninitializedPropertyAccessException
DCompilation error
Attempts:
2 left
💡 Hint
Lateinit variables must be initialized before use.
🔧 Debug
advanced
2: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)
AThrows KotlinNullPointerException
BPrints 0
CPrints null
DCompilation error
Attempts:
2 left
💡 Hint
The !! operator asserts non-null and throws if null.
🧠 Conceptual
expert
3: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()
Avar because it allows changing the list reference safely
Bval because it prevents reassignment but list contents can still change
Cval because it makes the list immutable
Dvar because it prevents concurrent modification exceptions
Attempts:
2 left
💡 Hint
val prevents changing the reference but does not make the object immutable.