0
0
Kotlinprogramming~20 mins

Data class copy and destructuring in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Data Class Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Kotlin code using data class copy?

Consider the following Kotlin data class and code snippet. What will be printed?

Kotlin
data class Person(val name: String, val age: Int)

fun main() {
    val original = Person("Alice", 30)
    val copy = original.copy(age = 35)
    println(copy)
}
APerson(name=Alice, age=35)
BPerson(name=Alice, age=30)
CPerson(name=Alice, age=0)
DPerson(name=, age=35)
Attempts:
2 left
💡 Hint

Remember that copy() creates a new object with the same properties unless you override them.

Predict Output
intermediate
2:00remaining
What is the output when destructuring a Kotlin data class?

Given the Kotlin data class and code below, what will be printed?

Kotlin
data class Point(val x: Int, val y: Int)

fun main() {
    val point = Point(10, 20)
    val (a, b) = point
    println("a = $a, b = $b")
}
Aa = 20, b = 10
Ba = 0, b = 0
Ca = Point(x=10, y=20), b = null
Da = 10, b = 20
Attempts:
2 left
💡 Hint

Destructuring extracts properties in the order they are declared in the data class.

🔧 Debug
advanced
2:00remaining
What error does this Kotlin code raise when copying a data class?

Examine the Kotlin code below. What error will it produce when run?

Kotlin
data class User(val username: String, val email: String)

fun main() {
    val user = User("bob", "bob@example.com")
    val newUser = user.copy(username = null)
    println(newUser)
}
ASyntaxError: unexpected null
BType mismatch: null cannot be a value of a non-null type String
CNullPointerException at runtime
DNo error, prints User(username=null, email=bob@example.com)
Attempts:
2 left
💡 Hint

Check the type of the username property and what null means in Kotlin.

Predict Output
advanced
2:00remaining
What is the output of destructuring with default values in Kotlin?

Consider this Kotlin data class and code. What will be printed?

Kotlin
data class Rectangle(val width: Int = 5, val height: Int = 10)

fun main() {
    val rect = Rectangle(height = 20)
    val (w, h) = rect
    println("Width: $w, Height: $h")
}
AWidth: 5, Height: 20
BWidth: 0, Height: 20
CWidth: 5, Height: 10
DWidth: 20, Height: 20
Attempts:
2 left
💡 Hint

Default values are used when arguments are not provided during object creation.

🧠 Conceptual
expert
2:00remaining
How many items are in the resulting map after copying and destructuring?

Given the Kotlin code below, how many entries does the resultMap contain?

Kotlin
data class Employee(val id: Int, val name: String, val department: String)

fun main() {
    val emp1 = Employee(1, "John", "Sales")
    val emp2 = emp1.copy(name = "Jane")
    val (id, name, dept) = emp2
    val resultMap = mapOf("id" to id, "name" to name, "department" to dept)
    println(resultMap.size)
}
A1
B2
C3
D0
Attempts:
2 left
💡 Hint

Count the number of key-value pairs added to the map.