Bird
0
0

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:
  1. Step 1: Recognize Kotlin data class syntax

    Kotlin uses 'data class' keyword to create classes that automatically generate useful methods.
  2. 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.
  3. Final Answer:

    data class Person(val name: String, val age: Int) -> Option C
  4. 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes