Book with title, author, and year propertiesBook called originalBook with exact valuesoriginalBook called updatedBook with the year changedtitle, author, and year from updatedBookTitle: [title], Author: [author], Year: [year]Book with title, author, and year propertiesBook called originalBook with exact valuesoriginalBook called updatedBook with the year changedtitle, author, and year from updatedBookTitle: [title], Author: [author], Year: [year]Book with title (String), author (String), and year (Int) properties. Then create a variable called originalBook and set it to Book("The Hobbit", "J.R.R. Tolkien", 1937).Use data class keyword to create the class. Then create originalBook with the exact values.
updatedBook by copying originalBook and changing the year to 1951 using the copy function.Use originalBook.copy(year = 1951) to create the new book.
title, author, and year from updatedBook.Use val (title, author, year) = updatedBook to get the values.
println in this exact format: Title: [title], Author: [author], Year: [year].Use println with an f-string style using $ to insert variables.
What does the copy() function do in a Kotlin data class?
copy()copy() function in Kotlin data classes creates a new instance with the same property values.copy() = new object with changes [OK]Which of the following is the correct syntax to copy a Kotlin data class Person and change only the age property?
data class Person(val name: String, val age: Int)
copy() function uses named arguments to change specific properties.person.copy(age = 30) changes only the age, keeping name same.What is the output of this Kotlin code?
data class Point(val x: Int, val y: Int)
fun main() {
val p1 = Point(5, 10)
val (a, b) = p1
val p2 = p1.copy(y = 20)
println("a = $a, b = $b")
println(p2)
}a = p1.x = 5 and b = p1.y = 10.p2 is a copy of p1 with y changed to 20, so Point(x=5, y=20).Find the error in this Kotlin code snippet:
data class User(val name: String, val age: Int)
fun main() {
val user1 = User("Alice", 25)
val (n, a) = user1.copy(age = 30)
println("Name: $n, Age: $a")
}copy() function is correctly called with age = 30.n = "Alice" and a = 30.Given this data class and list, how do you create a new list of Person objects with age increased by 1, using copy() and destructuring?
data class Person(val name: String, val age: Int)
val people = listOf(Person("Tom", 20), Person("Sue", 25))map with it.copy(age = it.age + 1) creates new objects with updated age.Pass the test and this pattern moves unknown