0
0
Swiftprogramming~20 mins

Functions returning tuples in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Tuple Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Swift function returning a tuple?
Consider the following Swift function that returns a tuple. What will be printed when calling result?
Swift
func getCoordinates() -> (x: Int, y: Int) {
    return (x: 10, y: 20)
}

let result = getCoordinates()
print("x: \(result.x), y: \(result.y)")
Ax: 0, y: 0
Bx: 20, y: 10
Cx: 10 y: 20
Dx: 10, y: 20
Attempts:
2 left
💡 Hint
Look at the tuple labels and values returned by the function.
🧠 Conceptual
intermediate
2:00remaining
What is the type of the tuple returned by this function?
Given this Swift function, what is the exact type of the returned tuple?
Swift
func getUserInfo() -> (name: String, age: Int) {
    return (name: "Alice", age: 30)
}
A(name: String, age: Int)
B(String, Int)
C(String, String)
D(Int, String)
Attempts:
2 left
💡 Hint
Check if the tuple has labels or not.
🔧 Debug
advanced
2:00remaining
Why does this Swift function cause a compile error?
Examine the function below. Why does it cause a compile error?
Swift
func calculate() -> (Int, Int) {
    return (x: 5, y: 10)
}
AThe tuple values are not integers
BThe function is missing a return statement
CThe return tuple has labels but the function return type does not specify labels
DThe function return type must be a class, not a tuple
Attempts:
2 left
💡 Hint
Check if tuple labels in return match the declared return type.
Predict Output
advanced
2:00remaining
What is the output of this function returning multiple values as a tuple?
What will be printed when running this Swift code?
Swift
func minMax(numbers: [Int]) -> (min: Int, max: Int)? {
    guard let first = numbers.first else { return nil }
    var currentMin = first
    var currentMax = first
    for number in numbers {
        if number < currentMin {
            currentMin = number
        } else if number > currentMax {
            currentMax = number
        }
    }
    return (min: currentMin, max: currentMax)
}

if let result = minMax(numbers: [3, 7, 2, 9, 4]) {
    print("Min is \(result.min), Max is \(result.max)")
} else {
    print("Empty array")
}
AMin is 2, Max is 9
BMin is 3, Max is 7
CMin is 9, Max is 2
DEmpty array
Attempts:
2 left
💡 Hint
The function finds the smallest and largest numbers in the array.
🧠 Conceptual
expert
2:00remaining
How can you access tuple elements returned by a Swift function without using labels?
Given a Swift function that returns an unlabeled tuple, how do you access the elements?
Swift
func getValues() -> (Int, String) {
    return (42, "Answer")
}

let values = getValues()
AUse values[0] and values[1]
BUse values.0 for the first element and values.1 for the second element
CUse values.first and values.second
DUse values.x and values.y
Attempts:
2 left
💡 Hint
Unlabeled tuples use numeric indices starting at zero.