Bird
Raised Fist0
Android Kotlinmobile~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which keyword should you use in Kotlin for a variable whose value will never change after assignment?
easy
A. let
B. var
C. val
D. const

Solution

  1. Step 1: Understand Kotlin variable keywords

    val is used for read-only variables, meaning their value cannot change once assigned.
  2. Step 2: Compare with other keywords

    var allows changing the value; const is not a Kotlin keyword for variables; let is from other languages like Swift or JavaScript.
  3. Final Answer:

    val -> Option C
  4. Quick Check:

    Immutable variable = val [OK]
Hint: Use val for fixed values, var for changeable ones [OK]
Common Mistakes:
  • Confusing val and var
  • Using const which is not Kotlin variable keyword
  • Thinking let is Kotlin keyword
2. Which of the following is the correct way to declare a nullable String variable in Kotlin?
easy
A. var name: String? = null
B. var name: String?
C. var name: String = null
D. var name: String = "null"

Solution

  1. Step 1: Understand nullable type syntax

    In Kotlin, adding ? after the type means the variable can hold null.
  2. Step 2: Check initialization

    Nullable variables should be initialized with null if no value is assigned yet.
  3. Final Answer:

    var name: String? = null -> Option A
  4. Quick Check:

    Nullable variable syntax = var name: String? = null [OK]
Hint: Use ? after type and assign null to allow null values [OK]
Common Mistakes:
  • Assigning null to non-nullable type
  • Forgetting ? for nullable types
  • Using string "null" instead of null
3. What will be the output of this Kotlin code?
var count: Int? = 5
count = null
println(count)
medium
A. null
B. 0
C. 5
D. Compilation error

Solution

  1. Step 1: Analyze variable declaration

    count is declared as nullable Int (Int?) and initially set to 5.
  2. Step 2: Assign null and print

    Then count is assigned null, which is allowed for nullable types, so printing it outputs null.
  3. Final Answer:

    null -> Option A
  4. Quick Check:

    Nullable variable assigned null prints null [OK]
Hint: Nullable variables can hold null and print it directly [OK]
Common Mistakes:
  • Expecting 5 after reassignment
  • Thinking null prints 0
  • Assuming compilation error for null assignment
4. Identify the error in this Kotlin code snippet:
val message: String = null
println(message)
medium
A. No error, code runs fine
B. val cannot be used with String type
C. println cannot print val variables
D. Cannot assign null to non-nullable type String

Solution

  1. Step 1: Check variable type and assignment

    message is declared as non-nullable String but assigned null, which is not allowed.
  2. Step 2: Understand Kotlin null safety rules

    Non-nullable types cannot hold null; to allow null, type must be String?.
  3. Final Answer:

    Cannot assign null to non-nullable type String -> Option D
  4. Quick Check:

    Non-nullable String cannot be null [OK]
Hint: Non-nullable types cannot hold null values [OK]
Common Mistakes:
  • Thinking val disallows null but var allows
  • Assuming println can't print val
  • Ignoring Kotlin's null safety rules
5. You want to create a variable that can change its value but sometimes hold no value at all. Which declaration is correct in Kotlin?
hard
A. val data: Int = null
B. var data: Int? = null
C. val data: Int? = 0
D. var data: Int = null

Solution

  1. Step 1: Understand mutability and nullability

    The variable must be changeable, so var is needed, and it must allow null, so type must be nullable with ?.
  2. Step 2: Check each option

    val data: Int = null uses val (immutable) and assigns null to non-nullable type (error). var data: Int? = null uses var and nullable type with null initial value (correct). val data: Int? = 0 uses val (immutable) but nullable type with 0 initial value (not changeable). var data: Int = null uses var but non-nullable type assigned null (error).
  3. Final Answer:

    var data: Int? = null -> Option B
  4. Quick Check:

    Mutable and nullable = var with ? [OK]
Hint: Use var for changeable and ? for nullable types [OK]
Common Mistakes:
  • Using val when variable should change
  • Assigning null to non-nullable type
  • Confusing val and var usage
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard