Bird
0
0

How can you create a range of even numbers from 2 to 20 using Swift range operators and stride?

hard📝 Application Q9 of 15
Swift - Operators and Expressions
How can you create a range of even numbers from 2 to 20 using Swift range operators and stride?
AAll of the above
Bfor i in 2...20 { if i % 2 == 0 { print(i) } }
Cfor i in 2..<21 { if i % 2 == 0 { print(i) } }
Dfor i in stride(from: 2, through: 20, by: 2) { print(i) }
Step-by-Step Solution
Solution:
  1. Step 1: Understand stride usage

    stride(from:through:by:) creates a sequence stepping by 2 from 2 to 20 inclusive.
  2. Step 2: Using range with condition

    Options B and C use ranges with a condition to print even numbers.
  3. Step 3: Confirm all options work

    All options correctly print even numbers from 2 to 20.
  4. Final Answer:

    All of the above -> Option A
  5. Quick Check:

    Multiple ways to print even numbers using ranges [OK]
Quick Trick: Use stride or filter with range for even numbers [OK]
Common Mistakes:
  • Forgetting to include 20
  • Using wrong stride parameters
  • Ignoring condition in loops

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes