0
0
Android Kotlinmobile~10 mins

Data classes in Android Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a data class named User with two properties.

Android Kotlin
data class User(val name: String, val [1]: Int)
Drag options to blanks, or click blank then click option'
Aage
Bheight
Cweight
Demail
Attempts:
3 left
💡 Hint
Common Mistakes
Using a property name that does not match the type Int.
Forgetting to declare the property inside the parentheses.
2fill in blank
medium

Complete the code to create an instance of the User data class.

Android Kotlin
val user = User(name = "Alice", [1] = 30)
Drag options to blanks, or click blank then click option'
Aweight
Bheight
Cemail
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Using a property name not defined in the data class.
Mixing up property names.
3fill in blank
hard

Fix the error in the copy function call to change the age of the user.

Android Kotlin
val olderUser = user.copy([1] = 31)
Drag options to blanks, or click blank then click option'
Aage
Bheight
Cweight
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to change a property not defined in the data class.
Using incorrect property names in the copy function.
4fill in blank
hard

Fill both blanks to define a data class with a default value for age.

Android Kotlin
data class User(val name: String, val age: Int = [1]) {
  fun isAdult() = age [2] 18
}
Drag options to blanks, or click blank then click option'
A18
B>
C<
D21
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong comparison operator.
Setting an unrealistic default age.
5fill in blank
hard

Fill all three blanks to create a data class with a method that returns a greeting message.

Android Kotlin
data class User(val name: String, val age: Int) {
  fun greet() = "Hello, my name is [1] and I am [2] years [3]."
}
Drag options to blanks, or click blank then click option'
Aname
Bold
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing property names with string literals.
Using incorrect words for the greeting.