What is the difference between val and var in Kotlin properties?
val declares a read-only property (like a constant). You cannot change its value after initialization. var declares a mutable property, which means you can change its value later.
Click to reveal answer
beginner
Can a val property be assigned a value after the object is created?
No. A val property must be assigned once during initialization and cannot be reassigned later.
Click to reveal answer
beginner
How do you declare a mutable property named age of type Int in Kotlin?
You declare it using var age: Int. This means you can change the value of age anytime.
Click to reveal answer
beginner
What happens if you try to reassign a val property in Kotlin?
The compiler will give an error because val properties are read-only and cannot be reassigned after initialization.
Click to reveal answer
beginner
Example: What will this code print?
val name = "Anna"
// name = "Bob" // What if uncommented?
The code will print nothing because it only declares name. If you uncomment the reassignment, the compiler will show an error because name is a val and cannot be changed.
Click to reveal answer
Which keyword declares a property that can be changed after initialization?
Avar
Bval
Cconst
Dlet
✗ Incorrect
var declares a mutable property that can be reassigned. val is read-only.
What happens if you try to assign a new value to a val property?
AIt works fine
BValue silently changes
CRuntime error
DCompiler error
✗ Incorrect
The Kotlin compiler prevents reassignment of val properties, causing a compile-time error.
How do you declare a read-only property in Kotlin?
Aval
Bvar
Clet
Dconst
✗ Incorrect
val declares a read-only property that cannot be reassigned.
Which of these is true about var properties?
AThey cannot be changed after initialization
BThey must be initialized with a value
CThey can be reassigned any number of times
DThey are constants
✗ Incorrect
var properties are mutable and can be reassigned multiple times.
If you want a property that never changes after creation, which keyword do you use?
Avar
Bval
Cmutable
Dfinal
✗ Incorrect
val creates a read-only property that cannot be changed after initialization.
Explain the difference between val and var properties in Kotlin with examples.
Think about constants versus variables in everyday life.
You got /5 concepts.
What error do you get if you try to change a val property after it is set? Why does Kotlin enforce this?
Consider why some things in life should not change once set.
You got /4 concepts.
Practice
(1/5)
1. What is the main difference between val and var properties in Kotlin?
easy
A. val can be changed anytime, var is read-only after initialization.
B. val is read-only after initialization, var can be changed anytime.
C. val and var both can be changed anytime.
D. val and var are exactly the same.
Solution
Step 1: Understand val behavior
val properties are assigned once and cannot be reassigned later.
Step 2: Understand var behavior
var properties can be reassigned multiple times after initialization.
Final Answer:
val is read-only after initialization, var can be changed anytime. -> Option B
Quick Check:
val = read-only, var = mutable [OK]
Hint: Remember: val = fixed, var = variable [OK]
Common Mistakes:
Confusing val as mutable
Thinking var is read-only
Believing val and var behave the same
2. Which of the following is the correct way to declare a mutable property named age of type Int in Kotlin?
easy
A. var age: Int = 25
B. val age: Int = 25
C. val age = 25
D. var age: Int
Solution
Step 1: Identify mutable property keyword
Mutable properties use var keyword.
Step 2: Check initialization
Property must be initialized or declared nullable; here age is initialized with 25.
Final Answer:
var age: Int = 25 -> Option A
Quick Check:
Mutable property uses var [OK]
Hint: Use var for changeable properties [OK]
Common Mistakes:
Using val for mutable property
Not initializing non-nullable property
Omitting type without initialization
3. What will be the output of the following Kotlin code?
class Person {
val name = "Anna"
var age = 30
}
fun main() {
val p = Person()
println(p.name)
p.age = 31
println(p.age)
}
medium
A. Anna
30
B. Anna
null
C. Error: Cannot assign to val property
D. Anna
31
Solution
Step 1: Check val name usage
name is a val, so it prints "Anna" and cannot be changed.
Step 2: Check var age reassignment
age is a var, so it can be changed from 30 to 31 before printing.
Final Answer:
Anna
31 -> Option D
Quick Check:
val prints original, var prints updated [OK]
Hint: val prints original, var can be updated [OK]
Common Mistakes:
Expecting error on changing var
Thinking val can be reassigned
Confusing printed values
4. Identify the error in the following Kotlin code:
class Car {
val model: String
var year: Int
init {
model = "Tesla"
year = 2020
}
}
fun main() {
val car = Car()
car.model = "BMW"
println(car.model)
}
medium
A. Cannot assign a new value to val property 'model'.
B. Property 'year' is not initialized.
C. Missing constructor parameters.
D. No error, code runs fine.
Solution
Step 1: Check val model reassignment
model is declared as val, so it cannot be reassigned after initialization.
Step 2: Verify property initialization
model and year are both initialized in init, so no initialization error.
Final Answer:
Cannot assign a new value to val property 'model'. -> Option A
Quick Check:
val properties cannot be reassigned [OK]
Hint: val properties cannot be changed after init [OK]
Common Mistakes:
Trying to reassign val property
Ignoring uninitialized var warnings
Assuming val can be reassigned
5. You want to create a Kotlin class Book where the title should never change after creation, but the price can be updated. Which declaration is correct?
hard
A. class Book(val title: String, val price: Double)
B. class Book(var title: String, var price: Double)
C. class Book(val title: String, var price: Double)
D. class Book(var title: String, val price: Double)
Solution
Step 1: Determine immutability for title
title should not change, so declare it with val.
Step 2: Determine mutability for price
price can change, so declare it with var.
Final Answer:
class Book(val title: String, var price: Double) -> Option C
Quick Check:
Immutable title = val, mutable price = var [OK]
Hint: Use val for fixed, var for changeable properties [OK]
Common Mistakes:
Using var for title which should be fixed
Using val for price which should be changeable
Mixing up val and var keywords
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.