0
0
Swiftprogramming~20 mins

Closures as function parameters in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Closures Master
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 code using a closure parameter?

Consider this Swift function that takes a closure as a parameter and calls it:

func greetUser(name: String, action: (String) -> String) {
    let message = action(name)
    print(message)
}

greetUser(name: "Anna") { userName in
    return "Hello, \(userName)!"
}

What will be printed when this code runs?

Swift
func greetUser(name: String, action: (String) -> String) {
    let message = action(name)
    print(message)
}

greetUser(name: "Anna") { userName in
    return "Hello, \(userName)!"
}
AHello, userName!
BHello, Anna!
CgreetUser(name: "Anna")
DError: Missing return statement
Attempts:
2 left
💡 Hint

Look at how the closure uses the parameter userName inside the string.

Predict Output
intermediate
2:00remaining
What does this Swift code print when using a trailing closure?

Examine this Swift code that uses a trailing closure syntax:

func performOperation(_ a: Int, _ b: Int, operation: (Int, Int) -> Int) {
    let result = operation(a, b)
    print(result)
}

performOperation(4, 5) { x, y in
    x * y
}

What is the output?

Swift
func performOperation(_ a: Int, _ b: Int, operation: (Int, Int) -> Int) {
    let result = operation(a, b)
    print(result)
}

performOperation(4, 5) { x, y in
    x * y
}
AError: Missing return in closure
B9
C45
D20
Attempts:
2 left
💡 Hint

Remember that the closure multiplies the two numbers.

🔧 Debug
advanced
2:00remaining
What error does this Swift code produce?

Look at this Swift function call with a closure parameter:

func calculate(value: Int, operation: (Int) -> Int) {
    let result = operation(value)
    print(result)
}

calculate(value: 10) { number in
    if number > 5 {
        number * 2
    }
}

What error will this code cause when compiled?

Swift
func calculate(value: Int, operation: (Int) -> Int) {
    let result = operation(value)
    print(result)
}

calculate(value: 10) { number in
    if number > 5 {
        number * 2
    }
}
AError: Missing return in closure
BRuntime error: nil value
CNo error, prints 20
DSyntax error: unexpected if statement
Attempts:
2 left
💡 Hint

Check if the closure always returns a value.

🧠 Conceptual
advanced
2:00remaining
Which option correctly describes escaping closures in Swift?

In Swift, what does it mean when a closure parameter is marked with @escaping?

AThe closure can be stored and called after the function returns
BThe closure must be called immediately inside the function
CThe closure cannot capture variables from its surrounding context
DThe closure is automatically executed on the main thread
Attempts:
2 left
💡 Hint

Think about when the closure might be used relative to the function call.

Predict Output
expert
3:00remaining
What is the output of this Swift code using nested closures?

Analyze this Swift code with nested closures passed as parameters:

func outerFunction(action: (Int) -> (Int) -> Int) {
    let innerClosure = action(3)
    let result = innerClosure(4)
    print(result)
}

outerFunction { x in
    return { y in
        x + y
    }
}

What will be printed?

Swift
func outerFunction(action: (Int) -> (Int) -> Int) {
    let innerClosure = action(3)
    let result = innerClosure(4)
    print(result)
}

outerFunction { x in
    return { y in
        x + y
    }
}
A12
BError: Cannot convert closure type
C7
D34
Attempts:
2 left
💡 Hint

Trace the values of x and y through the closures.