What does the closed range operator ... do in Swift?
The closed range operator ... creates a range that includes both the start and end values. For example, 1...3 includes 1, 2, and 3.
Click to reveal answer
beginner
What is the difference between ... and ..< operators?
The ... operator creates a closed range including the end value, while the ..< operator creates a half-open range that excludes the end value. For example, 1...3 includes 3, but 1..<3 does not include 3.
Click to reveal answer
beginner
How would you use the half-open range operator ..< in a for-loop?
You can use ..< to loop from a start number up to but not including an end number. For example, for i in 0..<5 loops with i values 0, 1, 2, 3, and 4.
Click to reveal answer
beginner
What happens if you use 1...1 in Swift?
Using 1...1 creates a range with a single value: 1. It includes the start and end which are the same.
Click to reveal answer
intermediate
Can you use range operators with types other than integers in Swift?
Yes, range operators can be used with any type that conforms to the Comparable protocol, such as characters or floating-point numbers.
Click to reveal answer
What values does the range 2...5 include?
A2, 3, 4
B2, 3, 4, 5
C3, 4, 5
D2, 3, 5
✗ Incorrect
The closed range operator ... includes both the start and end values, so it includes 2, 3, 4, and 5.
Which operator excludes the end value in Swift ranges?
A..<
B...
C=>
D<..
✗ Incorrect
The half-open range operator ..< excludes the end value.
What will the loop for i in 0..<3 print?
A0 1 2
B0 1 2 3
C1 2 3
D1 2
✗ Incorrect
The loop runs from 0 up to but not including 3, so it prints 0, 1, and 2.
Is 5...5 a valid range in Swift?
ANo, ranges must have at least two values
BNo, start and end cannot be equal
CYes, it includes only 5
DYes, it includes 5 and 6
✗ Incorrect
A closed range with the same start and end is valid and includes that single value.
Can you create a range of characters using ... in Swift?
ANo, ranges only work with numbers
BOnly with strings, not characters
COnly with integers converted to characters
DYes, if characters are comparable
✗ Incorrect
Range operators work with any Comparable type, including characters.
Explain the difference between the closed range operator ... and the half-open range operator ..< in Swift.
Think about whether the last number is included or not.
You got /4 concepts.
Describe how you would use range operators in a Swift for-loop to iterate over numbers 1 to 5.
Remember which operator includes or excludes the end.
You got /4 concepts.
Practice
(1/5)
1. What does the Swift range operator 1...5 represent?
easy
A. A range including numbers 1, 2, 3, 4, and 5
B. A range including numbers 1, 2, 3, and 4 only
C. A range excluding number 1 but including 5
D. A range excluding both 1 and 5
Solution
Step 1: Understand the closed range operator
The ... operator creates a range that includes both the start and end values.
Step 2: Apply to the given range
For 1...5, it includes 1, 2, 3, 4, and 5.
Final Answer:
A range including numbers 1, 2, 3, 4, and 5 -> Option A
Quick Check:
1...5 includes 5 [OK]
Hint: Remember: ... includes end number, ..< excludes it [OK]
Common Mistakes:
Thinking ... excludes the last number
Confusing ... with ..<
Assuming ..< includes the end number
2. Which of the following is the correct syntax to create a half-open range from 0 to 9 in Swift?
easy
A. 0...9
B. 0..<10
C. 0..9
D. 0...10
Solution
Step 1: Identify half-open range syntax
The half-open range operator in Swift is ..<, which includes the start but excludes the end.
Step 2: Apply to the range 0 to 9
To include numbers 0 through 9, use 0..<10 because 10 is excluded.
Final Answer:
0..<10 -> Option B
Quick Check:
..< excludes end [OK]
Hint: Use ..< to exclude the last number in range [OK]
Common Mistakes:
Using ... when half-open range is needed
Writing 0..9 which is invalid syntax
Confusing end number inclusion
3. What will be the output of the following Swift code?
for i in 3..<6 {
print(i, terminator: ",")
}
medium
A. 3,4,5,6,
B. 4,5,6,
C. 3,4,5,
D. 3,4,5,6
Solution
Step 1: Understand the range used in the loop
The range 3..<6 includes 3, 4, and 5 but excludes 6.
Step 2: Trace the loop output
The loop prints 3, then 4, then 5, each followed by a comma.
Final Answer:
3,4,5, -> Option C
Quick Check:
..<6 excludes 6 [OK]
Hint: ..< excludes end; loop stops before 6 [OK]
Common Mistakes:
Including 6 in output
Missing commas in output
Confusing terminator usage
4. The following Swift code has an error. What is the problem?
let range = 1..5
for num in range {
print(num)
}
medium
A. The range operator .. is invalid in Swift
B. The loop variable num is not declared
C. The print statement syntax is incorrect
D. The range should start from 0
Solution
Step 1: Check the range operator used
The code uses 1..5, which is not a valid range operator in Swift.
Step 2: Identify correct range operators
Swift uses ... for closed ranges and ..< for half-open ranges, but not ...
Final Answer:
The range operator .. is invalid in Swift -> Option A
Quick Check:
Invalid operator .. causes error [OK]
Hint: Use ... or ..<, never .. alone [OK]
Common Mistakes:
Using .. instead of ... or ..<
Assuming .. is a valid operator
Ignoring compiler error messages
5. You want to create a Swift array containing numbers from 5 to 15, excluding 15, and then print only the even numbers. Which code snippet correctly uses range operators and filtering?
hard
A. let numbers = Array(5...15).filter { $0 % 2 == 0 }
print(numbers)
B. let numbers = Array(5..15).filter { $0 % 2 == 0 }
print(numbers)
C. let numbers = Array(5..<14).filter { $0 % 2 == 0 }
print(numbers)
D. let numbers = Array(5..<15).filter { $0 % 2 == 0 }
print(numbers)
Solution
Step 1: Choose correct range excluding 15
To exclude 15, use the half-open range 5..<15, which includes 5 up to 14.
Step 2: Filter even numbers
The filter condition { $0 % 2 == 0 } selects even numbers from the array.
Step 3: Verify other options
let numbers = Array(5...15).filter { $0 % 2 == 0 }
print(numbers) includes 15, let numbers = Array(5..15).filter { $0 % 2 == 0 }
print(numbers) uses invalid operator .., and let numbers = Array(5..<14).filter { $0 % 2 == 0 }
print(numbers) uses 5..<14 which goes only up to 13, missing the even number 14.
Final Answer:
let numbers = Array(5..<15).filter { $0 % 2 == 0 }
print(numbers) -> Option D
Quick Check:
..< excludes end, filter selects even [OK]
Hint: Use ..< to exclude end, then filter even numbers [OK]
Common Mistakes:
Using ... to include 15 mistakenly
Using invalid .. operator
Not filtering correctly for even numbers
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.