Bird
0
0

What happens if you try to change an array inside a Swift for-in loop iterating over it?

easy📝 Conceptual Q2 of 15
Swift - Loops
What happens if you try to change an array inside a Swift for-in loop iterating over it?
ASwift prevents the change and shows a compile-time error
BThe loop skips the changed elements automatically
CThe array changes without any warning
DThe program compiles but crashes at runtime
Step-by-Step Solution
Solution:
  1. Step 1: Recall Swift's compile-time safety checks

    Swift checks for unsafe mutations during loops before running the program.
  2. Step 2: Identify the error type

    Trying to modify the array inside the loop causes a compile-time error, stopping unsafe code.
  3. Final Answer:

    Swift prevents the change and shows a compile-time error -> Option A
  4. Quick Check:

    Swift loop mutation = Compile-time error [OK]
Quick Trick: Swift stops unsafe changes before running code [OK]
Common Mistakes:
  • Assuming runtime crashes instead of compile errors
  • Believing arrays change silently during loops
  • Thinking loops skip changed elements automatically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes