Bird
0
0

Which of the following correctly declares a mutable variable age with an initial value of 30 in Swift?

easy📝 Conceptual Q2 of 15
Swift - Variables and Constants
Which of the following correctly declares a mutable variable age with an initial value of 30 in Swift?
Aage var = 30
Blet age = 30
Cvar age: String = 30
Dvar age = 30
Step-by-Step Solution
Solution:
  1. Step 1: Syntax for mutable variable

    Use var followed by the variable name and assignment to declare a mutable variable.
  2. Step 2: Check type compatibility

    Assigning 30 (an Int) to age is valid if type is inferred or declared as Int.
  3. Final Answer:

    var age = 30 -> Option D
  4. Quick Check:

    Mutable variable declaration uses var [OK]
Quick Trick: Use var name = value for mutable variables [OK]
Common Mistakes:
  • Using let instead of var for mutable variables
  • Incorrect type assignment (e.g., String for Int)
  • Wrong syntax order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes