Bird
Raised Fist0
Swiftprogramming~20 mins

Capturing values from context in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Swift Closure Capture 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 closure capturing example?

Consider this Swift code where a closure captures a variable from its context. What will be printed?

Swift
var count = 0
let incrementer = { count += 1; print(count) }
incrementer()
incrementer()
print(count)
A2\n2\n2
B1\n1\n0
C0\n1\n2
D1\n2\n2
Attempts:
2 left
💡 Hint

Closures capture variables by reference, so changes inside affect the original variable.

❓ Predict Output
intermediate
1:30remaining
What does this Swift closure print when capturing a constant?

Look at this Swift code where a closure captures a constant. What will be printed?

Swift
let number = 5
let printNumber = { print(number) }
printNumber()
ACompilation error
B0
C5
Dnil
Attempts:
2 left
💡 Hint

Constants can be captured by closures and used inside them.

🔧 Debug
advanced
2:30remaining
Why does this Swift closure capture cause unexpected output?

Examine this Swift code where a closure captures a loop variable. Why does it print the same number multiple times?

Swift
var closures: [() -> Void] = []
for i in 1...3 {
    closures.append { print(i) }
}
closures.forEach { $0() }
AAll closures capture the same variable <code>i</code> by reference, so they print the final value 4.
BEach closure captures a different copy of <code>i</code>, so they print 1, 2, 3 respectively.
CThe closures print 1, 1, 1 because <code>i</code> is reset each loop.
DThe code causes a runtime error because <code>i</code> is out of scope.
Attempts:
2 left
💡 Hint

Think about how Swift captures loop variables in closures.

❓ Predict Output
advanced
1:30remaining
What is the output of this Swift closure with capture list?

What will this Swift code print? It uses a capture list to capture a variable by value.

Swift
var value = 10
let closure = { [value] in print(value) }
value = 20
closure()
A10
BCompilation error
C20
Dnil
Attempts:
2 left
💡 Hint

Capture lists let you capture a variable's value at the time the closure is created.

🧠 Conceptual
expert
2:00remaining
Which statement about Swift closure captures is true?

Choose the correct statement about how Swift closures capture values from their context.

AClosures always capture variables by value, so changes outside do not affect the closure.
BClosures capture variables by reference by default, but capture lists can force capture by value.
CClosures cannot capture constants from their surrounding context.
DCapturing variables in closures always causes a memory leak.
Attempts:
2 left
💡 Hint

Think about how capture lists affect variable capturing.

Practice

(1/5)
1. What does it mean when a Swift closure captures values from context?
easy
A. The closure only works with global variables
B. The closure creates new variables unrelated to the surrounding scope
C. The closure cannot access any variables outside its own body
D. The closure keeps a reference to variables from its surrounding scope

Solution

  1. Step 1: Understand closure context

    A closure can access variables defined outside its own body, in the surrounding scope.
  2. Step 2: Capturing means referencing

    Capturing means the closure keeps a reference to those external variables so it can use them later.
  3. Final Answer:

    The closure keeps a reference to variables from its surrounding scope -> Option D
  4. Quick Check:

    Closure captures = keeps reference [OK]
Hint: Closures remember variables around them by reference [OK]
Common Mistakes:
  • Thinking closures create new independent variables
  • Believing closures cannot access outside variables
  • Confusing capture with copying values
2. Which of the following is the correct syntax to capture a value by copy in a Swift closure?
easy
A. let closure = { [value] in print(value) }
B. let closure = { [value = value] in print(value) }
C. let closure = { value in print(value) }
D. let closure = { capture value in print(value) }

Solution

  1. Step 1: Recognize capture list syntax

    To capture by value, Swift uses a capture list with syntax like [var = value].
  2. Step 2: Match correct syntax

    let closure = { [value = value] in print(value) } uses [value = value] which copies the current value into the closure.
  3. Final Answer:

    let closure = { [value = value] in print(value) } -> Option B
  4. Quick Check:

    Capture by copy uses [var = var] syntax [OK]
Hint: Use capture list with assignment to copy value [OK]
Common Mistakes:
  • Omitting capture list brackets
  • Using capture list without assignment
  • Writing invalid keywords like 'capture'
3. What will be printed by this Swift code?
var x = 10
let closure = { print(x) }
x = 20
closure()
medium
A. 20
B. nil
C. Compilation error
D. 10

Solution

  1. Step 1: Understand reference capture

    The closure captures x by reference, so it uses the current value when called.
  2. Step 2: Check value at call time

    Since x was changed to 20 before calling the closure, it prints 20.
  3. Final Answer:

    20 -> Option A
  4. Quick Check:

    Reference capture prints updated value [OK]
Hint: Closures print current variable value unless captured by copy [OK]
Common Mistakes:
  • Assuming closure prints original value 10
  • Expecting compilation error due to variable change
  • Thinking closure prints nil
4. Identify the error in this Swift closure code:
var count = 0
let increment = { count += 1 }
increment()
print(count)
medium
A. No error; code runs and prints 1
B. Closure must declare 'count' as inout parameter
C. Cannot modify 'count' inside closure without 'self.'
D. Closure cannot capture variables declared outside

Solution

  1. Step 1: Check variable mutability

    count is a variable (var), so it can be modified inside the closure.
  2. Step 2: Verify closure behavior

    The closure increments count by 1, then printing count outputs 1.
  3. Final Answer:

    No error; code runs and prints 1 -> Option A
  4. Quick Check:

    Mutable var can be changed inside closure [OK]
Hint: Vars can be changed inside closures without extra syntax [OK]
Common Mistakes:
  • Thinking 'self.' is required outside classes
  • Believing inout needed for closure variables
  • Assuming closures cannot modify external vars
5. You want a Swift closure to capture the value of a variable at the moment the closure is created, so later changes to the variable don't affect the closure. How do you do this?
hard
A. Declare the variable as a constant (let) before the closure
B. Use a global variable instead of a local one
C. Use a capture list to copy the variable value inside the closure
D. Modify the variable inside the closure before using it

Solution

  1. Step 1: Understand default capture behavior

    By default, closures capture variables by reference, so changes affect the closure.
  2. Step 2: Use capture list to copy value

    Using a capture list like [value = variable] copies the current value into the closure, isolating it from later changes.
  3. Final Answer:

    Use a capture list to copy the variable value inside the closure -> Option C
  4. Quick Check:

    Capture list copies value to freeze it [OK]
Hint: Capture list copies value to freeze it at closure creation [OK]
Common Mistakes:
  • Thinking 'let' alone freezes variable for closure
  • Using global variables to avoid capture issues
  • Modifying variable inside closure doesn't freeze value
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard