Bird
0
0

Which of the following Kotlin variable declarations uses type inference correctly?

easy📝 Syntax Q12 of 15
Kotlin - Variables and Type System
Which of the following Kotlin variable declarations uses type inference correctly?
Aval number: String = 10
Bval number: Int = "10"
Cval number = 10
Dval number =
Step-by-Step Solution
Solution:
  1. Step 1: Check each declaration for correctness

    val number = 10 assigns 10 to 'number' without specifying type, so compiler infers Int type correctly.
  2. Step 2: Identify errors in other options

    val number: Int = "10" assigns a String to Int type explicitly - error. val number: String = 10 assigns Int to String type - error. val number = is incomplete syntax.
  3. Final Answer:

    val number = 10 -> Option C
  4. Quick Check:

    Correct syntax with inferred type = val number = 10 [OK]
Quick Trick: Look for variable with value but no explicit type [OK]
Common Mistakes:
MISTAKES
  • Mixing types in assignment
  • Leaving declaration incomplete
  • Assigning wrong type to explicit type

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes