Challenge - 5 Problems
Data Class Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate2:00remaining
Correct syntax for a Kotlin data class
Which option correctly defines a Kotlin data class named
Person with properties name (String) and age (Int)?Attempts:
2 left
💡 Hint
Remember the correct order is
data class ClassName(...) with property types declared.✗ Incorrect
In Kotlin, a data class is declared with the keyword
data before class. Properties must have types and be declared inside the primary constructor.❓ ui_behavior
intermediate2:00remaining
Output of toString() on a Kotlin data class instance
Given this data class:
What is the output of:
data class User(val username: String, val id: Int)
What is the output of:
println(User("alice", 42))Android Kotlin
data class User(val username: String, val id: Int) fun main() { println(User("alice", 42)) }
Attempts:
2 left
💡 Hint
Data classes automatically generate a
toString() method showing property names and values.✗ Incorrect
Kotlin data classes generate a
toString() method that prints the class name and all properties with their values in the format ClassName(prop1=value1, prop2=value2).❓ lifecycle
advanced2:00remaining
Copying and modifying a Kotlin data class instance
Given this data class and instance:
Which option creates a copy of
data class Book(val title: String, val author: String, val year: Int)
val original = Book("1984", "Orwell", 1949)Which option creates a copy of
original but changes the year to 1950?Android Kotlin
data class Book(val title: String, val author: String, val year: Int) fun main() { val original = Book("1984", "Orwell", 1949) val copy = /* fill here */ println(copy) }
Attempts:
2 left
💡 Hint
Data classes have a built-in
copy() method to create modified copies.✗ Incorrect
The
copy() method allows creating a new instance with some properties changed. You specify the property name and new value as a named argument.🔧 Debug
advanced2:00remaining
Error caused by missing property types in data class
What error will this Kotlin code produce?
data class Car(val make, val model: String)
Android Kotlin
data class Car(val make, val model: String)Attempts:
2 left
💡 Hint
Every property in a data class constructor must declare its type.
✗ Incorrect
Kotlin requires all constructor properties to have explicit types. Missing type for 'make' causes a compilation error.
🧠 Conceptual
expert2:00remaining
Equality check between Kotlin data class instances
Consider these two instances:
What is the result of
data class Point(val x: Int, val y: Int) val p1 = Point(1, 2) val p2 = Point(1, 2)
What is the result of
p1 == p2?Android Kotlin
data class Point(val x: Int, val y: Int) fun main() { val p1 = Point(1, 2) val p2 = Point(1, 2) println(p1 == p2) }
Attempts:
2 left
💡 Hint
Data classes automatically generate
equals() to compare property values.✗ Incorrect
Kotlin data classes override
equals() to compare all properties for equality, so two instances with same property values are equal.