Bird
0
0

Which of the following is the correct syntax for a safe Swift for-in loop over an array named numbers?

easy📝 Syntax Q12 of 15
Swift - Loops
Which of the following is the correct syntax for a safe Swift for-in loop over an array named numbers?
Aloop number in numbers { print(number) }
Bfor (int i = 0; i < numbers.count; i++) { print(numbers[i]) }
Cfor number to numbers { print(number) }
Dfor number in numbers { print(number) }
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct Swift loop syntax

    Swift uses for variable in collection { } syntax for safe iteration.
  2. Step 2: Check each option

    Only for number in numbers { print(number) } matches correct syntax; A uses invalid 'loop', B is C-style (not Swift), C uses invalid 'to' keyword.
  3. Final Answer:

    for number in numbers { print(number) } -> Option D
  4. Quick Check:

    Swift for-in syntax = for number in numbers { print(number) } [OK]
Quick Trick: Use 'for item in collection' for safe Swift loops [OK]
Common Mistakes:
  • Using C-style for loops in Swift
  • Using incorrect keywords like 'loop' or 'to'
  • Forgetting 'in' keyword

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes