0
0
Kotlinprogramming~5 mins

Var for mutable references in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does var mean in Kotlin?

var declares a variable whose value can change (mutable).

Click to reveal answer
beginner
How do you declare a variable that cannot change in Kotlin?

Use val to declare an immutable (read-only) variable.

Click to reveal answer
beginner
What happens if you try to reassign a val variable?

Kotlin will give a compile error because val variables cannot be reassigned.

Click to reveal answer
beginner
Example: <br>var count = 5<br>count = 10<br>What is the value of count after reassignment?

The value of count is 10 because var allows changing the value.

Click to reveal answer
beginner
Why use var instead of val?

Use var when you need to change the value later, like a counter or user input.

Click to reveal answer
Which keyword declares a mutable variable in Kotlin?
Aconst
Bval
Cvar
Dlet
What happens if you try to assign a new value to a val variable?
ACompile-time error
BIt works fine
CRuntime error
DValue changes silently
Which of these is a correct way to declare a mutable integer variable with initial value 3?
Aconst number = 3
Bval number = 3
Clet number = 3
Dvar number = 3
If you want a variable that never changes, which keyword do you use?
Avar
Bval
Cmutable
Dconst
What is the main difference between var and val?
A<code>var</code> is mutable; <code>val</code> is immutable
B<code>var</code> is immutable; <code>val</code> is mutable
CBoth are mutable
DBoth are immutable
Explain the difference between var and val in Kotlin.
Think about whether the variable's value can change after you set it.
You got /4 concepts.
    Give an example where you would use var instead of val.
    Think about situations where the value needs to update.
    You got /4 concepts.