0
0
Swiftprogramming~20 mins

Escaping closures (@escaping) in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Escaping Closure Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a function with an escaping closure
What will be printed when this Swift code runs?
Swift
func performTask(completion: @escaping () -> Void) {
    DispatchQueue.global().async {
        print("Task started")
        completion()
    }
}

performTask {
    print("Task completed")
}

print("Function called")
AFunction called\nTask started\nTask completed
BTask started\nTask completed\nFunction called
CFunction called\nTask completed\nTask started
DTask completed\nTask started\nFunction called
Attempts:
2 left
💡 Hint
Think about how async code runs in the background and when the print statements execute.
🧠 Conceptual
intermediate
1:30remaining
Why use @escaping in Swift closures?
Which statement best explains why the @escaping attribute is required for a closure parameter in Swift?
ABecause the closure is called after the function returns, so it escapes the function's scope.
BBecause the closure modifies variables outside its own scope.
CBecause the closure is guaranteed to run synchronously within the function.
DBecause the closure is optional and might not be called.
Attempts:
2 left
💡 Hint
Think about when the closure is executed relative to the function call.
🔧 Debug
advanced
2:00remaining
Identify the error with closure escaping
What error will this Swift code produce and why?
Swift
func fetchData(completion: () -> Void) {
    DispatchQueue.global().async {
        completion()
    }
}

fetchData {
    print("Data fetched")
}
ANo error, prints 'Data fetched' asynchronously
BError: Missing return statement in closure
CRuntime crash due to closure escaping
DError: Closure use of non-escaping parameter 'completion' may allow it to escape
Attempts:
2 left
💡 Hint
Check the closure parameter declaration and how it's used inside an async block.
📝 Syntax
advanced
1:30remaining
Correct syntax for an escaping closure parameter
Which option correctly declares a function with an escaping closure parameter in Swift?
Afunc loadData(completion: escaping () -> Void) {}
Bfunc loadData(@escaping completion: () -> Void) {}
Cfunc loadData(completion: @escaping () -> Void) {}
Dfunc loadData(completion: () -> Void @escaping) {}
Attempts:
2 left
💡 Hint
Remember the position of @escaping in the parameter declaration.
🚀 Application
expert
2:30remaining
Determine the final value after escaping closure modifies a variable
What is the value of 'counter' after this Swift code runs?
Swift
var counter = 0

func incrementLater(completion: @escaping () -> Void) {
    DispatchQueue.global().async {
        counter += 1
        completion()
    }
}

incrementLater {
    print("Increment done")
}

// Wait briefly to let async code finish
Thread.sleep(forTimeInterval: 0.1)

print(counter)
A0
B1
C2
DCompilation error
Attempts:
2 left
💡 Hint
Consider that the increment happens asynchronously and the main thread waits before printing.