Bird
0
0

What will be the output of this Swift code?

medium📝 Predict Output Q4 of 15
Swift - Loops
What will be the output of this Swift code?
let nums = [10, 20, 30]
for num in nums {
  print(num)
}
A10 20 30
B30 20 10
C10 20 30 10
DError: Cannot iterate over array
Step-by-Step Solution
Solution:
  1. Step 1: Understand the for-in loop behavior

    The loop prints each element in the array in order.
  2. Step 2: Trace the output

    It prints 10, then 20, then 30 each on a new line.
  3. Final Answer:

    10 20 30 -> Option A
  4. Quick Check:

    for-in loop output = elements in order [OK]
Quick Trick: for-in prints elements in order by default [OK]
Common Mistakes:
  • Expecting reverse order output
  • Thinking it prints duplicates
  • Assuming iteration causes errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes