Bird
Raised Fist0
Kotlinprogramming~20 mins

Var for mutable references in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Kotlin Mutable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Kotlin code using var?

Consider the following Kotlin code snippet. What will be printed?

Kotlin
fun main() {
    var number = 10
    number = 20
    println(number)
}
A20
B10
CCompilation error
DRuntime exception
Attempts:
2 left
💡 Hint

Remember that var allows the variable to be changed.

❓ Predict Output
intermediate
2:00remaining
What happens when you reassign a val variable?

What will happen when running this Kotlin code?

Kotlin
fun main() {
    val text = "Hello"
    text = "World"
    println(text)
}
AWorld
BRuntime exception
CHello
DCompilation error
Attempts:
2 left
💡 Hint

val means the variable cannot be reassigned.

❓ Predict Output
advanced
2:00remaining
What is the output when modifying a mutable list declared with var?

Analyze this Kotlin code and determine the output.

Kotlin
fun main() {
    var list = mutableListOf(1, 2, 3)
    list.add(4)
    println(list)
}
A[1, 2, 3, 4]
BRuntime exception
C[1, 2, 3]
DCompilation error
Attempts:
2 left
💡 Hint

Mutable lists can be changed even if declared with var.

❓ Predict Output
advanced
2:00remaining
What error occurs when trying to reassign a val mutable list?

What will happen when running this Kotlin code?

Kotlin
fun main() {
    val list = mutableListOf(1, 2, 3)
    list = mutableListOf(4, 5, 6)
    println(list)
}
A[4, 5, 6]
B[1, 2, 3]
CCompilation error
DRuntime exception
Attempts:
2 left
💡 Hint

val means the reference cannot be changed, even if the object is mutable.

🧠 Conceptual
expert
2:00remaining
Why use var for mutable references in Kotlin?

Which statement best explains why var is used for mutable references in Kotlin?

A<p>Because <code>var</code> makes the object itself immutable.</p>
B<p>Because <code>var</code> allows changing the reference to point to a different object or value.</p>
C<p>Because <code>var</code> prevents any changes to the variable after initialization.</p>
D<p>Because <code>var</code> automatically synchronizes access in multithreaded code.</p>
Attempts:
2 left
💡 Hint

Think about what var allows you to do with a variable.

Practice

(1/5)
1. What does the var keyword do in Kotlin?
easy
A. Declares a class
B. Creates a constant value that cannot be changed
C. Creates a variable whose value can be changed later
D. Defines a function

Solution

  1. Step 1: Understand the role of var

    The var keyword declares a variable that can be changed after it is created.
  2. Step 2: Compare with other keywords

    val creates a constant (unchangeable) variable, so var is for mutable variables.
  3. Final Answer:

    Creates a variable whose value can be changed later -> Option C
  4. Quick Check:

    var means mutable variable [OK]
Hint: Remember: var = variable that can vary [OK]
Common Mistakes:
  • Confusing var with val (immutable)
  • Thinking var declares constants
  • Mixing var with function or class declarations
2. Which of the following is the correct way to declare a mutable integer variable named count with initial value 10 in Kotlin?
easy
A. val count: Int = 10
B. val count = 10
C. var count: Int
D. var count = 10

Solution

  1. Step 1: Identify mutable variable declaration

    To declare a mutable variable, use var with an initial value.
  2. Step 2: Check syntax correctness

    var count = 10 correctly declares a mutable integer variable with initial value 10.
  3. Final Answer:

    var count = 10 -> Option D
  4. Quick Check:

    Mutable variable needs var and initial value [OK]
Hint: Use var with initial value for mutable variables [OK]
Common Mistakes:
  • Using val instead of var for mutable variables
  • Declaring var without initial value or type
  • Confusing val and var syntax
3. What will be the output of this Kotlin code?
var x = 5
x = 10
println(x)
medium
A. 10
B. Compilation error
C. 5
D. Runtime error

Solution

  1. Step 1: Understand variable assignment

    Variable x is declared mutable with initial value 5, then changed to 10.
  2. Step 2: Print the current value of x

    The print statement outputs the updated value 10.
  3. Final Answer:

    10 -> Option A
  4. Quick Check:

    Mutable var allows reassignment, so output is 10 [OK]
Hint: var lets you change value; print latest value [OK]
Common Mistakes:
  • Thinking var variables cannot be reassigned
  • Expecting initial value to print
  • Confusing compilation and runtime errors
4. Identify the error in this Kotlin code snippet:
var name: String
name = "Alice"
name = 123
medium
A. Variable name must be declared with val
B. Cannot assign a number to a String variable
C. Missing initial value for var
D. Syntax error in variable declaration

Solution

  1. Step 1: Check variable type and assignments

    Variable name is declared as String, assigned "Alice" (valid), then assigned 123 (number).
  2. Step 2: Identify type mismatch error

    Assigning an integer to a String variable causes a type error.
  3. Final Answer:

    Cannot assign a number to a String variable -> Option B
  4. Quick Check:

    Type mismatch causes error [OK]
Hint: Check variable type before assigning new value [OK]
Common Mistakes:
  • Ignoring type mismatch errors
  • Thinking var requires initial value always
  • Confusing val and var usage
5. You want to keep track of a user's score that changes during the game. Which Kotlin code correctly declares and updates this score using var?
hard
A. var score = 0 score = score + 10 println(score)
B. val score = 0 score = score + 10 println(score)
C. var score: Int println(score)
D. val score: Int = 0 score += 10 println(score)

Solution

  1. Step 1: Declare a mutable variable with initial value

    Use var score = 0 to allow changes to score.
  2. Step 2: Update the score correctly

    Adding 10 to score with score = score + 10 works because score is mutable.
  3. Final Answer:

    var score = 0 score = score + 10 println(score) -> Option A
  4. Quick Check:

    Mutable var allows updating score [OK]
Hint: Use var for values that change, val for constants [OK]
Common Mistakes:
  • Using val for variables that need updating
  • Declaring var without initial value and using before assignment
  • Trying to update val variables causing errors
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