0
0
Swiftprogramming~20 mins

Generic function declaration in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Generic Function Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a generic swap function
What is the output of this Swift code that uses a generic function to swap two values?
Swift
func swapValues<T>(_ a: inout T, _ b: inout T) {
    let temp = a
    a = b
    b = temp
}

var x = 5
var y = 10
swapValues(&x, &y)
print("x = \(x), y = \(y)")
ACompilation error due to generic function
Bx = 5, y = 10
Cx = 0, y = 0
Dx = 10, y = 5
Attempts:
2 left
💡 Hint
Think about what the swap function does to the values passed by reference.
Predict Output
intermediate
2:00remaining
Output of generic function with different types
What is the output of this Swift code using a generic function that prints the type and value?
Swift
func printValue<T>(_ value: T) {
    print("Value: \(value), Type: \(type(of: value))")
}

printValue(42)
printValue("Hello")
AValue: 42, Type: String\nValue: Hello, Type: Int
BValue: 42, Type: Int\nValue: Hello, Type: String
CValue: 42, Type: Optional<Int>\nValue: Hello, Type: Optional<String>
DCompilation error due to type mismatch
Attempts:
2 left
💡 Hint
The generic function accepts any type and prints its value and type.
Predict Output
advanced
2:00remaining
Output of generic function with constraints
What is the output of this Swift code using a generic function constrained to Comparable?
Swift
func findMax<T: Comparable>(_ a: T, _ b: T) -> T {
    return a > b ? a : b
}

print(findMax(3, 7))
print(findMax("apple", "banana"))
ACompilation error due to missing Comparable conformance
B3\napple
C7\nbanana
DRuntime error due to type mismatch
Attempts:
2 left
💡 Hint
The function returns the greater of two Comparable values.
🔧 Debug
advanced
2:00remaining
Identify the error in this generic function declaration
What error does this Swift code produce when compiling?
Swift
func printFirstElement<T>(array: [T]) -> T {
    return array[0]
}

print(printFirstElement(array: []))
ARuntime error: Index out of range
BCompilation error: Missing return statement
CCompilation error: Generic type T not specified
DNo error, prints nil
Attempts:
2 left
💡 Hint
Consider what happens when accessing the first element of an empty array.
🧠 Conceptual
expert
2:00remaining
Understanding generic function type inference
Given the generic function below, what is the inferred type of T when calling foo(5) and foo("test") respectively?
Swift
func foo<T>(_ value: T) -> String {
    return "Value is \(value)"
}

// Calls:
// foo(5)
// foo("test")
AT is Int for foo(5) and String for foo("test")
BT is always Any for all inputs
CT is always String regardless of input
DCompilation error due to ambiguous type inference
Attempts:
2 left
💡 Hint
Generic functions infer T based on the argument type at each call.