0
0
Swiftprogramming~20 mins

Range operators (... and ..<) in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Range Operator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of inclusive range loop
What is the output of this Swift code using the inclusive range operator ...?
Swift
for i in 1...3 {
    print(i, terminator: ",")
}
A2,3,4,
B1,2,
C1,2,3,4,
D1,2,3,
Attempts:
2 left
💡 Hint
Inclusive range includes both start and end values.
Predict Output
intermediate
2:00remaining
Output of half-open range loop
What is the output of this Swift code using the half-open range operator ..<?
Swift
for i in 1..<4 {
    print(i, terminator: "-")
}
A1-2-3-
B1-2-3-4-
C2-3-4-
D1-2-
Attempts:
2 left
💡 Hint
Half-open range excludes the end value.
🔧 Debug
advanced
2:00remaining
Why does this range cause a runtime error?
Consider this Swift code snippet:
let numbers = [10, 20, 30]
for i in 0...numbers.count {
    print(numbers[i])
}
What error will this code cause when run?
ANo error, prints all elements
BIndex out of range runtime error
CSyntax error: missing colon
DType error: cannot subscript array with Int
Attempts:
2 left
💡 Hint
Check the range bounds against the array indices.
🧠 Conceptual
advanced
2:00remaining
Count of elements in a half-open range
How many elements are in the range 5..<10 in Swift?
A5
B6
C4
D10
Attempts:
2 left
💡 Hint
Count is end minus start for half-open ranges.
🚀 Application
expert
3:00remaining
Using range operators to slice arrays
Given let arr = ["a", "b", "c", "d", "e"], which option correctly extracts the subarray ["b", "c", "d"] using range operators?
Aarr[0..<3]
Barr[1...3]
Carr[1..<4]
Darr[2...4]
Attempts:
2 left
💡 Hint
Remember that array indices start at 0 and half-open ranges exclude the upper bound.