Bird
0
0

Given the Swift code:

medium📝 query result Q5 of 15
Swift - Collections
Given the Swift code:
let temperatures = [72, 68, 75]
temperatures[1] = 70
print(temperatures)

What will happen when this code runs?
AThe array prints without any changes.
BThe second element changes to 70 and prints the updated array.
CRuntime error when trying to assign a new value.
DCompilation error due to modifying an immutable array.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze let declaration

    let makes the array immutable.
  2. Step 2: Attempt to modify element

    Modifying an element is a mutation, which is disallowed.
  3. Final Answer:

    Compilation error due to modifying an immutable array. -> Option D
  4. Quick Check:

    let arrays cannot be mutated [OK]
Quick Trick: let arrays are immutable; element assignment fails [OK]
Common Mistakes:
  • Assuming element mutation is allowed on let arrays
  • Expecting runtime error instead of compile-time

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes