Complete the code to declare a read-only property.
class Person { val name: String = [1] }
The val keyword declares a read-only property. Here, name is assigned the value "Alice".
Complete the code to declare a mutable property.
class Car { var speed: Int = [1] }
val instead of var for mutable properties.The var keyword declares a mutable property. Here, speed is initialized to 0 and can be changed later.
Fix the error in the property declaration to make it mutable.
class Book { [1] title: String = "Kotlin Guide" }
val which makes the property read-only.fun which is for functions, not properties.Using var makes the property mutable, allowing its value to be changed after initialization.
Fill both blanks to declare a read-only property with a custom getter.
class Circle(val radius: Double) { val area: Double get() = [1] * radius [2] radius }
val inside the getter instead of a value.The area of a circle is calculated as π times radius squared. Here, Math.PI is multiplied by radius * radius.
Fill all three blanks to declare a mutable property with a backing field and custom setter.
class Temperature { var celsius: Double = [1] set(value) { field = if (value [2] [3]273.15) [3]273.15 else value } }
field inside the setter.The property celsius is initialized to 0.0. The setter checks if the new value is less than absolute zero (-273.15) and sets it to -273.15 if so; otherwise, it sets the value normally.