Bird
0
0

You want to safely remove all even numbers from an array while looping. Which approach respects Swift's loop safety?

hard📝 Application Q8 of 15
Swift - Loops
You want to safely remove all even numbers from an array while looping. Which approach respects Swift's loop safety?
AModify the array inside a while loop without checks
BRemove even numbers directly inside a for-in loop over the array
CUse a for-in loop and remove elements by index during iteration
DCreate a new array filtering out even numbers, then assign it back
Step-by-Step Solution
Solution:
  1. Step 1: Understand safe mutation

    Modifying a collection during iteration is unsafe in Swift.
  2. Step 2: Use filtering to create a new array

    Filtering creates a new array without modifying the original during iteration.
  3. Final Answer:

    Create a new array filtering out even numbers, then assign it back -> Option D
  4. Quick Check:

    Safe removal = Use filtered copy [OK]
Quick Trick: Filter to remove items safely, don't mutate in loop [OK]
Common Mistakes:
  • Removing items inside for-in loop
  • Modifying array by index during iteration
  • Ignoring Swift's safety rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes