Recall & Review
beginner
What does
val mean in Kotlin?val declares a read-only (immutable) reference. Once assigned, you cannot change what it points to.
Click to reveal answer
beginner
Can you reassign a new value to a
val variable in Kotlin?No, you cannot reassign a new value to a val variable. It is immutable after initialization.
Click to reveal answer
beginner
How is
val different from var in Kotlin?val is immutable (cannot be reassigned), while var is mutable (can be reassigned).
Click to reveal answer
beginner
Example: What happens if you try to reassign a
val variable?<br>val x = 10 x = 20
This will cause a compile-time error because x is declared with val and cannot be reassigned.
Click to reveal answer
intermediate
Does
val guarantee the object it points to is immutable?<p>No, <code>val</code> only means the reference cannot change. The object itself can still be mutable if its class allows it.</p>Click to reveal answer
What does
val declare in Kotlin?✗ Incorrect
val declares an immutable reference that cannot be reassigned.
Can you reassign a new value to a
val variable after initialization?✗ Incorrect
val variables cannot be reassigned after they are initialized.
Which keyword allows reassignment in Kotlin?
✗ Incorrect
var declares a mutable variable that can be reassigned.
If you declare
val list = mutableListOf(1, 2, 3), can you add elements to list?✗ Incorrect
val means the reference cannot change, but the mutable list object can be modified.
What error occurs if you try to reassign a
val variable?✗ Incorrect
Reassigning a val causes a compile-time error in Kotlin.
Explain what
val means in Kotlin and how it differs from var.Think about whether you can change the variable after setting it.
You got /4 concepts.
Describe a situation where using
val is better than var.Consider when you want to protect data from accidental changes.
You got /4 concepts.