Bird
Raised Fist0
Kotlinprogramming~20 mins

Data classes for value holders in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Data Class Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of data class copy with modification
What is the output of this Kotlin code using a data class and the copy function?
Kotlin
data class Point(val x: Int, val y: Int)

fun main() {
    val p1 = Point(3, 5)
    val p2 = p1.copy(y = 10)
    println(p2)
}
APoint(x=3, y=10)
BPoint(x=3, y=5)
CPoint(x=10, y=5)
DCompilation error
Attempts:
2 left
💡 Hint
The copy function creates a new instance with specified changes.
❓ Predict Output
intermediate
2:00remaining
Equality check between data class instances
What will be the output of this Kotlin program?
Kotlin
data class User(val name: String, val age: Int)

fun main() {
    val user1 = User("Alice", 30)
    val user2 = User("Alice", 30)
    println(user1 == user2)
}
Atrue
BRuntime exception
CCompilation error
Dfalse
Attempts:
2 left
💡 Hint
Data classes automatically implement equals based on properties.
🔧 Debug
advanced
2:00remaining
Why does this data class fail to compile?
This Kotlin data class code fails to compile. What is the cause?
Kotlin
data class Person(var name: String, val age: Int) {
    var name: String = name
}
AMissing primary constructor
BData classes cannot have var properties
CData classes cannot have properties with initializers
DProperty 'name' is declared twice causing a conflict
Attempts:
2 left
💡 Hint
Check how properties are declared in the primary constructor and class body.
🧠 Conceptual
advanced
1:30remaining
Number of components in a data class
Given this Kotlin data class, how many component functions are generated automatically?
Kotlin
data class Rectangle(val width: Int, val height: Int, val color: String)
A2
B3
C1
D0
Attempts:
2 left
💡 Hint
Each property declared in the primary constructor generates a component function.
❓ Predict Output
expert
2:30remaining
Output of destructuring declaration with data class
What is the output of this Kotlin program?
Kotlin
data class Book(val title: String, val author: String, val year: Int)

fun main() {
    val book = Book("1984", "Orwell", 1949)
    val (t, a, y) = book
    println("$t by $a, published in $y")
}
ABook(title=1984, author=Orwell, year=1949)
BCompilation error due to destructuring
C1984 by Orwell, published in 1949
DRuntime exception
Attempts:
2 left
💡 Hint
Data classes support destructuring declarations matching their properties.

Practice

(1/5)
1. What is the main purpose of a data class in Kotlin?
easy
A. To manage database connections
B. To create a user interface layout
C. To hold data with automatically generated functions like toString() and equals()
D. To perform complex mathematical calculations

Solution

  1. Step 1: Understand the role of data classes

    Data classes are designed to hold data and automatically provide useful functions like toString(), equals(), and hashCode().
  2. Step 2: Compare options with data class purpose

    Options A, B, and D describe unrelated tasks. Only To hold data with automatically generated functions like toString() and equals() matches the purpose of data classes.
  3. Final Answer:

    To hold data with automatically generated functions like toString() and equals() -> Option C
  4. Quick Check:

    Data class purpose = hold data simply [OK]
Hint: Data classes are for simple data holders with auto functions [OK]
Common Mistakes:
  • Confusing data classes with UI or database classes
  • Thinking data classes perform calculations
  • Ignoring the automatic function generation feature
2. Which of the following is the correct syntax to declare a Kotlin data class named Person with properties name (String) and age (Int)?
easy
A. data class Person(val name: String, val age: Int)
B. class Person(val name: String, val age: Int)
C. data Person(val name: String, val age: Int)
D. class data Person(name: String, age: Int)

Solution

  1. Step 1: Recall data class syntax

    In Kotlin, a data class is declared with the keyword data class followed by the class name and primary constructor with properties.
  2. Step 2: Check each option

    class Person(val name: String, val age: Int) misses the data keyword. data Person(val name: String, val age: Int) uses data without class. class data Person(name: String, age: Int) incorrectly orders keywords. Only data class Person(val name: String, val age: Int) is correct.
  3. Final Answer:

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

    Correct syntax = data class + name + properties [OK]
Hint: Start with 'data class' keyword for data classes [OK]
Common Mistakes:
  • Omitting the 'data' keyword
  • Swapping 'data' and 'class' keywords
  • Not using 'val' or 'var' for properties
3. What will be the output of this Kotlin code?
data class Point(val x: Int, val y: Int)

fun main() {
    val p1 = Point(3, 4)
    val p2 = Point(3, 4)
    println(p1 == p2)
    println(p1)
}
medium
A. false Point(x=3, y=4)
B. true Point(x=3, y=4)
C. true Point(3, 4)
D. false Point(3, 4)

Solution

  1. Step 1: Understand equality in data classes

    Data classes automatically generate equals() that compares property values, so p1 == p2 is true because both have same x and y.
  2. Step 2: Understand toString() output

    Data classes generate toString() that prints property names and values like Point(x=3, y=4).
  3. Final Answer:

    true Point(x=3, y=4) -> Option B
  4. Quick Check:

    Data class equality and toString() = true and Point(x=3, y=4) [OK]
Hint: Data classes compare by value and print with property names [OK]
Common Mistakes:
  • Assuming == compares references, not values
  • Expecting default toString() without property names
  • Confusing output format of toString()
4. Identify the error in this Kotlin data class declaration:
data class User(name: String, age: Int)
medium
A. Data classes cannot have more than one property
B. The class name should be lowercase
C. The keyword data should be after class
D. Missing val or var before properties in the constructor

Solution

  1. Step 1: Check primary constructor properties

    In data classes, properties in the primary constructor must be marked with val or var to become class properties.
  2. Step 2: Analyze the given code

    The code lacks val or var before name and age, so they are just parameters, not properties.
  3. Final Answer:

    Missing val or var before properties in the constructor -> Option D
  4. Quick Check:

    Constructor properties need val/var [OK]
Hint: Always add val or var before constructor properties [OK]
Common Mistakes:
  • Forgetting val/var in data class constructor
  • Thinking data keyword position is wrong
  • Assuming class names can be lowercase
5. You want to create a data class Book with properties title (String), author (String), and year (Int). You also want to create a copy of a Book instance but change only the year. Which Kotlin feature helps you do this easily?
hard
A. The copy() function automatically generated for data classes
B. Manually creating a new instance with all properties copied
C. Using inheritance to override the year property
D. Using a mutable list to store book properties

Solution

  1. Step 1: Recall data class features

    Data classes automatically generate a copy() function that creates a new instance copying all properties, allowing selective changes.
  2. Step 2: Match the feature to the problem

    The copy() function automatically generated for data classes describes the copy() function which fits the need to copy and change only year. Other options are less efficient or unrelated.
  3. Final Answer:

    The copy() function automatically generated for data classes -> Option A
  4. Quick Check:

    Use copy() to clone and modify data class instances [OK]
Hint: Use copy() to clone and change data class properties [OK]
Common Mistakes:
  • Not knowing about the copy() function
  • Trying to manually copy all properties
  • Confusing inheritance with copying
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