Bird
0
0

Which of the following is a correct explicit type declaration for a mutable variable holding a decimal number in Kotlin?

easy📝 Conceptual Q2 of 15
Kotlin - Variables and Type System
Which of the following is a correct explicit type declaration for a mutable variable holding a decimal number in Kotlin?
Avar price = "9.99"
Bval price: Double = 9.99
Cvar price: Double = 9.99
Dval price: Int = 9.99
Step-by-Step Solution
Solution:
  1. Step 1: Identify mutable variable syntax

    Mutable variables use var, so options with val are immutable.
  2. Step 2: Check type and value compatibility

    Price is a decimal, so Double is correct. var price: Double = 9.99 uses var price: Double = 9.99, which is valid.
  3. Final Answer:

    var price: Double = 9.99 -> Option C
  4. Quick Check:

    Mutable decimal variable = var with Double type [OK]
Quick Trick: Use var for mutable, val for immutable variables [OK]
Common Mistakes:
MISTAKES
  • Using val when mutability is needed
  • Assigning decimal to Int type
  • Using string type for numbers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes