How can you use Kotlin to create a simple data class representing a person with name and age?
hard📝 Application Q9 of 15
Kotlin - Basics and JVM Runtime
How can you use Kotlin to create a simple data class representing a person with name and age?
Astruct Person(name: String, age: Int)
Bclass Person { var name: String; var age: Int }
Cdata class Person(val name: String, val age: Int)
Drecord Person(String name, int age)
Step-by-Step Solution
Solution:
Step 1: Recognize Kotlin data class syntax
Kotlin uses 'data class' keyword to create classes that automatically generate useful methods.
Step 2: Compare other options
class Person { var name: String; var age: Int } is incomplete and lacks constructor. struct Person(name: String, age: Int) uses 'struct' which is not Kotlin. record Person(String name, int age) uses 'record' which is Java 17 feature, not Kotlin.
Final Answer:
data class Person(val name: String, val age: Int) -> Option C
Quick Check:
Kotlin data classes use 'data class' keyword [OK]
Quick Trick:Use 'data class' for simple data holders in Kotlin [OK]
Common Mistakes:
MISTAKES
Using Java or C syntax
Not using 'data' keyword
Missing constructor parameters
Master "Basics and JVM Runtime" in Kotlin
9 interactive learning modes - each teaches the same concept differently