0
0
Swiftprogramming~20 mins

Deinitializers for cleanup in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Deinitializer Mastery
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 deinitializer?
Consider this Swift class with a deinitializer. What will be printed when the instance is created and then set to nil?
Swift
class Printer {
    let message: String
    init(message: String) {
        self.message = message
        print("Init: \(message)")
    }
    deinit {
        print("Deinit: \(message)")
    }
}

var p: Printer? = Printer(message: "Hello")
p = nil
A
Init: Hello
Deinit: Hello
BDeinit: Hello
CInit: Hello
DNo output
Attempts:
2 left
💡 Hint
Think about when the deinitializer runs in Swift.
Predict Output
intermediate
2:00remaining
What happens if a class instance with a deinitializer is never set to nil?
Given this Swift code, what will be the output?
Swift
class Logger {
    deinit {
        print("Logger is being deinitialized")
    }
}

func createLogger() {
    let log = Logger()
    print("Logger created")
}

createLogger()
ANo output
B
Logger is being deinitialized
Logger created
CLogger created
D
Logger created
Logger is being deinitialized
Attempts:
2 left
💡 Hint
Think about the lifetime of local variables in functions.
🔧 Debug
advanced
2:30remaining
Why does this deinitializer not print anything?
Examine this Swift code. Why does the deinitializer message never appear?
Swift
class Resource {
    deinit {
        print("Resource cleaned up")
    }
}

var r1: Resource? = Resource()
var r2 = r1
r1 = nil
// r2 is still holding the instance
ABecause r2 still holds a strong reference, so the instance is not deallocated.
BBecause deinit only runs if both variables are nil at the same time.
CBecause the deinitializer is not called for optional variables.
DBecause the print statement is inside deinit and never executes.
Attempts:
2 left
💡 Hint
Think about how Swift manages memory with references.
Predict Output
advanced
2:30remaining
What is the output of this Swift code with multiple instances and deinitializers?
Analyze this Swift code and select the correct output sequence.
Swift
class Item {
    let id: Int
    init(id: Int) {
        self.id = id
        print("Init \(id)")
    }
    deinit {
        print("Deinit \(id)")
    }
}

var items: [Item]? = [Item(id: 1), Item(id: 2)]
items?.append(Item(id: 3))
items = nil
A
Init 1
Init 2
Init 3
Deinit 1
Deinit 2
Deinit 3
B
Init 1
Init 2
Init 3
C
Init 1
Init 2
Init 3
Deinit 3
Deinit 2
Deinit 1
D
Init 1
Init 2
Init 3
Deinit 1
Deinit 3
Deinit 2
Attempts:
2 left
💡 Hint
Think about the order of initialization and deinitialization of array elements.
🧠 Conceptual
expert
2:00remaining
Which statement about Swift deinitializers is true?
Choose the correct statement about deinitializers in Swift classes.
AStructs and enums can have deinitializers to clean up resources.
BDeinitializers are called automatically when the last strong reference to an instance is removed.
CYou can call a deinitializer explicitly to free resources early.
DA deinitializer can be called multiple times during an object's lifetime.
Attempts:
2 left
💡 Hint
Recall how Swift manages memory and object lifecycle.