Use var when you want to change the value of a variable later. It lets you keep a box where you can put different things over time.
Var for mutable references in Kotlin
Introduction
Syntax
Kotlin
var variableName: Type = initialValuevar means the variable can change.
You must give it a type or let Kotlin figure it out from the initial value.
Examples
age that can change and starts at 25.Kotlin
var age: Int = 25
name is a String from the value "Alice".Kotlin
var name = "Alice"
Kotlin
var temperature: Double temperature = 36.6
Sample Program
This program shows how a var variable counter changes its value step by step.
Kotlin
fun main() { var counter = 0 println("Counter starts at: $counter") counter = 5 println("Counter changed to: $counter") counter += 10 println("Counter after adding 10: $counter") }
Important Notes
Use var only when you need to change the value. Otherwise, use val for fixed values.
Changing a var variable means you can update your program's data as things happen.
Summary
var creates a variable that can change its value.
It is useful when you want to update information during your program.
Always give it a starting value or type so Kotlin knows what it holds.
Practice
1. What does the
var keyword do in Kotlin?easy
Solution
Step 1: Understand the role of
Thevarvarkeyword declares a variable that can be changed after it is created.Step 2: Compare with other keywords
valcreates a constant (unchangeable) variable, sovaris for mutable variables.Final Answer:
Creates a variable whose value can be changed later -> Option CQuick Check:
varmeans 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
Solution
Step 1: Identify mutable variable declaration
To declare a mutable variable, usevarwith an initial value.Step 2: Check syntax correctness
var count = 10correctly declares a mutable integer variable with initial value 10.Final Answer:
var count = 10 -> Option DQuick Check:
Mutable variable needsvarand 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
Solution
Step 1: Understand variable assignment
Variablexis declared mutable with initial value 5, then changed to 10.Step 2: Print the current value of
The print statement outputs the updated value 10.xFinal Answer:
10 -> Option AQuick 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
Solution
Step 1: Check variable type and assignments
Variablenameis declared as String, assigned "Alice" (valid), then assigned 123 (number).Step 2: Identify type mismatch error
Assigning an integer to a String variable causes a type error.Final Answer:
Cannot assign a number to a String variable -> Option BQuick 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
Solution
Step 1: Declare a mutable variable with initial value
Usevar score = 0to allow changes to score.Step 2: Update the score correctly
Adding 10 to score withscore = score + 10works because score is mutable.Final Answer:
var score = 0 score = score + 10 println(score) -> Option AQuick 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
10 questions ~5 mineasy hard
