Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a copy of the data class instance with a new age.
Kotlin
val person2 = person.[1](age = 30)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using clone() which is not available for Kotlin data classes.
Trying to use copyOf() which is not a data class function.
✗ Incorrect
The copy() function is used to create a copy of a data class instance with some properties changed.
2fill in blank
mediumComplete the code to destructure the data class into variables.
Kotlin
val (name, [1]) = person Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a property name not declared in the data class.
Mixing up the order of properties.
✗ Incorrect
Data classes automatically provide componentN() functions for destructuring. The second property is 'age'.
3fill in blank
hardFix the error in the copy function call to change the name.
Kotlin
val person3 = person.[1](name = "Alice")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using clone() or copyOf() which are not valid for data classes.
Misspelling the function name.
✗ Incorrect
The correct function to copy a data class with modifications is copy().
4fill in blank
hardFill both blanks to destructure the data class and print the values.
Kotlin
val ([1], [2]) = person println("Name: $[1], Age: $[2]")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using properties not declared in the data class.
Mixing up the order of variables.
✗ Incorrect
The first and second properties of the data class are 'name' and 'age', used for destructuring.
5fill in blank
hardFill all three blanks to copy the data class with a new name, destructure it, and print the values.
Kotlin
val person4 = person.[1](name = "Bob") val ([2], [3]) = person4 println("Name: $[2], Age: $[3]")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using clone() instead of copy().
Mixing up variable names in destructuring.
✗ Incorrect
Use copy() to create a new instance with a changed name, then destructure into name and age variables.