Complete the code to declare a data class named Person with two properties: name and age.
data class Person([1] name: String, val age: Int)
In Kotlin data classes, properties are usually declared with val to make them read-only.
Complete the code to create an instance of the Person data class with name "Alice" and age 30.
val person = Person([1] = "Alice", age = 30)
The first argument is the name property, so we assign "Alice" to name.
Fix the error in the code to correctly override the toString method in the data class.
data class Person(val name: String, val age: Int) { override fun [1](): String { return "Person(name=$name, age=$age)" } }
The method to override for string representation is toString().
Fill both blanks to create a copy of a Person instance with a new age.
val olderPerson = person.[1]([2] = person.age + 1)
The copy function creates a new instance with modified properties. We change the age property.
Fill all three blanks to create a data class with default values and a method that returns a greeting.
data class Person(val name: String = [1], val age: Int = [2]) { fun greet() = "Hello, my name is $[3]" }
Default name is set to "Guest", default age to 0, and the greet method uses the name property.