0
0
Kotlinprogramming~10 mins

Data class copy and destructuring in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Aclone
Bcopy
Cduplicate
DcopyOf
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.
2fill in blank
medium

Complete the code to destructure the data class into variables.

Kotlin
val (name, [1]) = person
Drag options to blanks, or click blank then click option'
Aage
Bweight
Cheight
Daddress
Attempts:
3 left
💡 Hint
Common Mistakes
Using a property name not declared in the data class.
Mixing up the order of properties.
3fill in blank
hard

Fix 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'
Acopy
Bduplicate
Cclone
DcopyOf
Attempts:
3 left
💡 Hint
Common Mistakes
Using clone() or copyOf() which are not valid for data classes.
Misspelling the function name.
4fill in blank
hard

Fill 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'
Aname
Bage
Cheight
Dweight
Attempts:
3 left
💡 Hint
Common Mistakes
Using properties not declared in the data class.
Mixing up the order of variables.
5fill in blank
hard

Fill 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'
Acopy
Bname
Cage
Dclone
Attempts:
3 left
💡 Hint
Common Mistakes
Using clone() instead of copy().
Mixing up variable names in destructuring.