0
0
Kotlinprogramming~20 mins

Properties with val and var in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kotlin Properties Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of val and var property modification
What is the output of this Kotlin code?
Kotlin
class Person {
    val name = "Alice"
    var age = 30
}

fun main() {
    val p = Person()
    // p.name = "Bob" // Uncommenting this line causes error
    p.age = 31
    println("Name: ${p.name}, Age: ${p.age}")
}
AName: Alice, Age: 31
BName: Bob, Age: 31
CName: Alice, Age: 30
DCompilation error due to val property modification
Attempts:
2 left
💡 Hint
Remember val means read-only property, var means mutable property.
Predict Output
intermediate
2:00remaining
Property mutability in data classes
What will be the output of this Kotlin code?
Kotlin
data class Point(val x: Int, var y: Int)

fun main() {
    val p = Point(10, 20)
    // p.x = 15 // Uncommenting causes error
    p.y = 25
    println("x = ${p.x}, y = ${p.y}")
}
ACompilation error due to val property reassignment
Bx = 15, y = 25
Cx = 10, y = 25
Dx = 10, y = 20
Attempts:
2 left
💡 Hint
val properties cannot be reassigned, var properties can.
🔧 Debug
advanced
2:00remaining
Identify the error with val property reassignment
What error does this Kotlin code produce?
Kotlin
class Car {
    val model = "Sedan"
}

fun main() {
    val c = Car()
    c.model = "SUV"
    println(c.model)
}
ARuntime error: Uninitialized property access
BCompilation error: Val cannot be reassigned
CCompilation error: Variable 'c' must be mutable
DNo error, prints: SUV
Attempts:
2 left
💡 Hint
val properties cannot be reassigned after initialization.
🧠 Conceptual
advanced
2:00remaining
Difference between val and var in Kotlin properties
Which statement correctly describes the difference between val and var properties in Kotlin?
Aval properties are immutable and cannot be reassigned; var properties are mutable and can be reassigned.
Bval properties can be reassigned only inside the class; var properties cannot be reassigned.
Cval properties are mutable only if declared inside a data class; var properties are always immutable.
Dval and var properties behave the same; the difference is only in naming convention.
Attempts:
2 left
💡 Hint
Think about whether you can change the value after it is set.
Predict Output
expert
3:00remaining
Output with val and var in inheritance
What is the output of this Kotlin code?
Kotlin
open class Animal {
    open val sound: String = ""
}

class Dog : Animal() {
    override val sound = "Bark"
}

class Cat : Animal() {
    override var sound = "Meow"
}

fun main() {
    val dog = Dog()
    val cat = Cat()
    println("Dog sound: ${dog.sound}")
    println("Cat sound: ${cat.sound}")
    cat.sound = "Purr"
    println("Cat new sound: ${cat.sound}")
}
A
Dog sound: 
Cat sound: Meow
Cat new sound: Purr
B
Dog sound: Bark
Cat sound: Meow
Compilation error on cat.sound reassignment
C
Dog sound: Bark
Cat sound: Meow
Cat new sound: Purr
DCompilation error: Cannot override val with var
Attempts:
2 left
💡 Hint
val can be overridden by val, var can be overridden by var, but val cannot be overridden by var and vice versa.