Bird
Raised Fist0
Android Kotlinmobile~20 mins

Data classes in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a data class in Kotlin?
easy
A. To hold data and automatically provide useful functions like toString() and equals()
B. To create user interface components
C. To manage app navigation between screens
D. To handle background tasks and threading

Solution

  1. Step 1: Understand the role of data classes

    Data classes are designed to store data and automatically generate functions like toString(), equals(), and hashCode().
  2. Step 2: Compare with other options

    Options B, C, and D relate to UI, navigation, and threading, which are not the purpose of data classes.
  3. Final Answer:

    To hold data and automatically provide useful functions like toString() and equals() -> Option A
  4. Quick Check:

    Data class purpose = hold data with auto functions [OK]
Hint: Data classes store data and auto-generate helper functions [OK]
Common Mistakes:
  • Confusing data classes with UI components
  • Thinking data classes manage app navigation
  • Assuming data classes handle threading
2. Which of the following is the correct syntax to declare a data class named Person with properties name (String) and age (Int)?
easy
A. class Person(val name: String, val age: Int)
B. data Person class(name: String, age: Int)
C. data class Person(val name: String, val age: Int)
D. class data Person(val name: String, val age: Int)

Solution

  1. Step 1: Recall correct data class syntax

    The correct syntax starts with data class followed by the class name and constructor parameters.
  2. Step 2: Check each option

    data class Person(val name: String, val age: Int) matches the correct syntax. Options A, B, and D have wrong order or missing keywords.
  3. Final Answer:

    data class Person(val name: String, val age: Int) -> Option C
  4. Quick Check:

    Data class syntax = data class ClassName(params) [OK]
Hint: Start with 'data class' keyword to declare data classes [OK]
Common Mistakes:
  • Omitting the 'data' keyword
  • Swapping order of 'class' and 'data'
  • Using incorrect parameter syntax
3. Given the data class:
data class Book(val title: String, val author: String)

What will be the output of:
val book1 = Book("Kotlin Basics", "Anna")
val book2 = Book("Kotlin Basics", "Anna")
println(book1 == book2)
medium
A. false
B. Runtime exception
C. Compilation error
D. true

Solution

  1. Step 1: Understand equality in data classes

    Data classes automatically generate equals() to compare property values, so two objects with same data are equal.
  2. Step 2: Evaluate the code

    Both book1 and book2 have identical title and author, so book1 == book2 returns true.
  3. Final Answer:

    true -> Option D
  4. Quick Check:

    Data class equality compares properties = true [OK]
Hint: Data class equality checks property values, not references [OK]
Common Mistakes:
  • Thinking '==' compares references, not values
  • Expecting false because objects are different instances
  • Confusing with regular class equality
4. What is wrong with this data class declaration?
data class User(val name: String, var age: Int) {
  fun copyWithNewAge(newAge: Int): User {
    return copy(age = newAge)
  }
}
medium
A. The copy function is automatically generated and can be used here
B. The copy function cannot be used inside the class
C. Data classes cannot have functions inside them
D. The age property cannot be var in a data class

Solution

  1. Step 1: Understand the copy function in data classes

    Kotlin automatically generates a copy function for data classes to create modified copies.
  2. Step 2: Check usage inside the class

    Using copy inside the class is valid and common to create new instances with changed properties.
  3. Final Answer:

    The copy function is automatically generated and can be used here -> Option A
  4. Quick Check:

    copy() is auto-generated and usable inside data class [OK]
Hint: copy() is built-in and usable inside data classes [OK]
Common Mistakes:
  • Thinking copy() is unavailable inside the class
  • Believing data classes cannot have functions
  • Assuming var properties are disallowed
5. You want to create a data class Employee with properties id (Int), name (String), and department (String). You also want to exclude department from the automatically generated equals() and hashCode() methods because it can change often. How can you achieve this?
hard
A. Make department a var property and mark it with @Transient
B. Use data class Employee(val id: Int, val name: String) and add var department: String inside the class body
C. You cannot exclude properties from equals() and hashCode() in data classes
D. Declare department inside the class body, not in the primary constructor

Solution

  1. Step 1: Understand how data classes generate equals and hashCode

    Only properties declared in the primary constructor are used in equals() and hashCode().
  2. Step 2: Exclude a property by declaring it outside the constructor

    Declaring department inside the class body excludes it from these methods.
  3. Final Answer:

    Use data class Employee(val id: Int, val name: String) and add var department: String inside the class body -> Option B
  4. Quick Check:

    Only constructor properties affect equals/hashCode [OK]
Hint: Exclude property by declaring it outside constructor [OK]
Common Mistakes:
  • Assuming @Transient excludes from equals/hashCode
  • Trying to exclude constructor properties directly
  • Believing all properties must be in constructor
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard