Bird
0
0

Which Kotlin declaration uses type inference correctly for a list of strings?

easy📝 Conceptual Q2 of 15
Kotlin - Variables and Type System
Which Kotlin declaration uses type inference correctly for a list of strings?
Aval names: Int = listOf("Anna", "Bob", "Cara")
Bval names = listOf("Anna", "Bob", "Cara")
Cval names = listOf(1, 2, 3)
Dval names: String = listOf("Anna", "Bob", "Cara")
Step-by-Step Solution
Solution:
  1. Step 1: Check the assigned value type

    listOf("Anna", "Bob", "Cara") creates a list of strings.
  2. Step 2: Verify type inference correctness

    val names = listOf("Anna", "Bob", "Cara") lets Kotlin infer the type as List, which is correct. Other options mismatch types.
  3. Final Answer:

    val names = listOf("Anna", "Bob", "Cara") -> Option B
  4. Quick Check:

    Type inference matches list content type [OK]
Quick Trick: Let Kotlin infer collection types from elements [OK]
Common Mistakes:
MISTAKES
  • Forcing wrong explicit types
  • Mixing element types in list
  • Assigning list to single String or Int

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes