Bird
0
0

What does the following Swift code do?

easy📝 Conceptual Q11 of 15
Swift - Loops
What does the following Swift code do?
let numbers = [1, 2, 3]
for number in numbers {
    print(number)
}
APrints each number in the array one by one
BPrints the entire array once
CCauses a syntax error
DPrints the sum of the numbers
Step-by-Step Solution
Solution:
  1. Step 1: Understand the for-in loop structure

    The loop goes through each element in the array named 'numbers'.
  2. Step 2: Observe the print statement inside the loop

    Each element is printed on its own line as the loop runs.
  3. Final Answer:

    Prints each number in the array one by one -> Option A
  4. Quick Check:

    for number in numbers prints each number [OK]
Quick Trick: For-in loops print items one at a time [OK]
Common Mistakes:
  • Thinking it prints the whole array at once
  • Confusing the loop variable with the collection
  • Expecting a sum instead of individual prints

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes