0
0
Swiftprogramming~20 mins

Defer statement for cleanup in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Defer 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 defer?

Consider the following Swift code snippet. What will be printed when it runs?

Swift
func testDefer() {
    print("Start")
    defer {
        print("Deferred 1")
    }
    defer {
        print("Deferred 2")
    }
    print("End")
}
testDefer()
A
Start
End
Deferred 2
Deferred 1
B
Start
End
Deferred 1
Deferred 2
C
Deferred 1
Deferred 2
Start
End
D
Start
Deferred 1
Deferred 2
End
Attempts:
2 left
💡 Hint

Remember that defer statements run in reverse order when the function exits.

Predict Output
intermediate
2:00remaining
What happens if defer modifies a variable?

What is the value of count after running this Swift code?

Swift
func countTest() -> Int {
    var count = 0
    defer {
        count += 1
    }
    count += 2
    return count
}
let result = countTest()
print(result)
A0
B1
C2
D3
Attempts:
2 left
💡 Hint

Defer runs after the return value is set but before the function fully exits.

🔧 Debug
advanced
2:00remaining
Why does this defer block not execute?

Examine the code below. Why does the defer block not print anything?

Swift
func earlyExit() {
    defer {
        print("Cleanup")
    }
    return
    print("This will not print")
}
earlyExit()
AThe defer block is skipped because return is before it.
BThe defer block runs before return and prints "Cleanup".
CThe defer block runs but print output is buffered and not shown.
DThe defer block is ignored because it is after the return statement.
Attempts:
2 left
💡 Hint

Defer always runs when exiting the current scope, even if return is called.

Predict Output
advanced
2:00remaining
What is the output when defer modifies a reference type?

What will be printed by this Swift code?

Swift
class Counter {
    var value = 0
}

func modifyCounter() {
    let counter = Counter()
    defer {
        counter.value += 10
        print("Deferred value: \(counter.value)")
    }
    counter.value += 5
    print("Current value: \(counter.value)")
}
modifyCounter()
A
Current value: 15
Deferred value: 15
B
Current value: 5
Deferred value: 5
C
Deferred value: 15
Current value: 5
D
Current value: 5
Deferred value: 15
Attempts:
2 left
💡 Hint

Defer runs after the print of current value, so it sees the updated value.

🧠 Conceptual
expert
2:00remaining
How many times is the defer block executed in this loop?

Consider this Swift code. How many times will the defer block run?

Swift
func loopDefer() {
    for i in 1...3 {
        defer {
            print("Deferred in iteration \(i)")
        }
        print("Loop iteration \(i)")
    }
}
loopDefer()
A3 times, once after each loop iteration
B6 times, twice per iteration
C0 times, defer inside loop is ignored
D1 time, after the entire loop finishes
Attempts:
2 left
💡 Hint

Defer runs when the current scope exits. What is the scope of the defer inside the loop?