0
0
Android Kotlinmobile~20 mins

Data classes in Android 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!
📝 Syntax
intermediate
2: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)?
Aclass data Person(val name: String, val age: Int)
Bdata class Person(val name: String, val age: Int)
Cdata Person class(val name: String, val age: Int)
Ddata class Person(name: String, age: Int)
Attempts:
2 left
💡 Hint
Remember the correct order is data class ClassName(...) with property types declared.
ui_behavior
intermediate
2:00remaining
Output of toString() on a Kotlin data class instance
Given this data class:
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))
}
AUser(alice, 42)
BUser@42
CUser(username=alice, id=42)
Dalice 42
Attempts:
2 left
💡 Hint
Data classes automatically generate a toString() method showing property names and values.
lifecycle
advanced
2:00remaining
Copying and modifying a Kotlin data class instance
Given this data class and instance:
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)
}
Aoriginal.copy(year = 1950)
BBook(original.title, original.author, 1950)
Coriginal.clone(year = 1950)
Doriginal.copy(1950)
Attempts:
2 left
💡 Hint
Data classes have a built-in copy() method to create modified copies.
🔧 Debug
advanced
2: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)
AError: Property 'make' must have a type
BNo error, compiles fine
CError: Missing parentheses in class declaration
DError: Duplicate property names
Attempts:
2 left
💡 Hint
Every property in a data class constructor must declare its type.
🧠 Conceptual
expert
2:00remaining
Equality check between Kotlin data class instances
Consider these two instances:
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)
}
AReference equality check, result depends on JVM
Bfalse
CCompilation error
Dtrue
Attempts:
2 left
💡 Hint
Data classes automatically generate equals() to compare property values.