Bird
0
0

You want to create a Kotlin data class to hold user info with automatic equals, hashCode, and toString. Which code is correct?

hard📝 Application Q8 of 15
Kotlin - Basics and JVM Runtime
You want to create a Kotlin data class to hold user info with automatic equals, hashCode, and toString. Which code is correct?
Arecord User(val name: String, val age: Int)
Bdata class User(val name: String, val age: Int)
Cclass User(val name: String, val age: Int)
Dstruct User(val name: String, val age: Int)
Step-by-Step Solution
Solution:
  1. Step 1: Identify Kotlin's data class syntax

    data class keyword creates a class with auto-generated equals, hashCode, and toString.
  2. Step 2: Check other options for correctness

    class lacks auto-generated methods; record and struct are not Kotlin keywords.
  3. Final Answer:

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

    Data class syntax = C [OK]
Quick Trick: Use 'data class' for auto-generated methods in Kotlin [OK]
Common Mistakes:
MISTAKES
  • Using plain class instead of data class
  • Confusing Kotlin with Java records
  • Using non-existent keywords like struct

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes