Challenge - 5 Problems
ARC Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this ARC example?
Consider this Swift code using ARC. What will be printed?
Swift
class Person { let name: String init(name: String) { self.name = name print("\(name) is initialized") } deinit { print("\(name) is being deinitialized") } } var reference1: Person? = Person(name: "Alice") var reference2 = reference1 reference1 = nil print("Reference1 is nil") reference2 = nil print("Reference2 is nil")
Attempts:
2 left
💡 Hint
Think about when the last strong reference to the object is removed.
✗ Incorrect
The Person instance is created and initialized first. Then reference1 is set to nil, but reference2 still holds the object, so it is not deinitialized yet. When reference2 is set to nil, no strong references remain, so deinit is called.
🧠 Conceptual
intermediate1:30remaining
Which statement about ARC is true?
Choose the correct statement about Automatic Reference Counting (ARC) in Swift.
Attempts:
2 left
💡 Hint
Think about how ARC manages memory automatically.
✗ Incorrect
ARC automatically tracks and frees memory when no strong references to an object remain. Weak references do not increase the reference count. ARC does not require manual memory management and works with class instances (reference types), not value types.
❓ Predict Output
advanced2:00remaining
What error occurs with this ARC cycle?
What happens when this Swift code runs?
Swift
class Node { var next: Node? deinit { print("Node is being deinitialized") } } var node1: Node? = Node() var node2: Node? = Node() node1!.next = node2 node2!.next = node1 node1 = nil node2 = nil
Attempts:
2 left
💡 Hint
Think about what happens when two objects strongly reference each other.
✗ Incorrect
The two Node instances strongly reference each other, creating a cycle. Even after setting node1 and node2 to nil, the objects remain in memory because their reference counts never reach zero. No deinit is called, causing a memory leak.
❓ Predict Output
advanced2:00remaining
What is printed with weak references?
What will this Swift code print?
Swift
class Person { let name: String init(name: String) { self.name = name print("\(name) initialized") } deinit { print("\(name) deinitialized") } } class Apartment { weak var tenant: Person? } var person: Person? = Person(name: "Bob") var apartment: Apartment? = Apartment() apartment!.tenant = person person = nil print("Person is nil")
Attempts:
2 left
💡 Hint
Weak references do not keep the object alive.
✗ Incorrect
The Person instance is created and assigned to apartment's weak tenant property. When person is set to nil, no strong references remain, so deinit is called. The weak reference does not keep the object alive.
🧠 Conceptual
expert1:30remaining
Why use unowned references in ARC?
Which is the best reason to use an unowned reference instead of a weak reference in Swift ARC?
Attempts:
2 left
💡 Hint
Think about the difference between weak and unowned references regarding nil values.
✗ Incorrect
Unowned references are used when the referenced object is expected to always exist during the lifetime of the reference, so it is not optional and does not become nil. Weak references are optional and become nil automatically. Using unowned incorrectly can cause runtime crashes.