0
0
Swiftprogramming~20 mins

ARC overview for memory management in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ARC Mastery Badge
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 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")
A
Alice is initialized
Reference1 is nil
Alice is being deinitialized
Reference2 is nil
B
Alice is initialized
Alice is being deinitialized
Reference1 is nil
Reference2 is nil
C
Reference1 is nil
Reference2 is nil
Alice is initialized
Alice is being deinitialized
D
Alice is initialized
Reference1 is nil
Reference2 is nil
Alice is being deinitialized
Attempts:
2 left
💡 Hint
Think about when the last strong reference to the object is removed.
🧠 Conceptual
intermediate
1:30remaining
Which statement about ARC is true?
Choose the correct statement about Automatic Reference Counting (ARC) in Swift.
AARC increases the reference count when a weak reference is created.
BARC requires manual calls to free memory after using an object.
CARC only works with value types like structs and enums.
DARC automatically frees memory when there are no more strong references to an object.
Attempts:
2 left
💡 Hint
Think about how ARC manages memory automatically.
Predict Output
advanced
2: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
ACompile-time error: Cannot assign to property
B
Node is being deinitialized
Node is being deinitialized
CNo output, memory leak occurs due to strong reference cycle.
DRuntime error: Stack overflow
Attempts:
2 left
💡 Hint
Think about what happens when two objects strongly reference each other.
Predict Output
advanced
2: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")
A
Bob initialized
Person is nil
B
Bob initialized
Bob deinitialized
Person is nil
C
Person is nil
Bob deinitialized
D
Bob initialized
Bob deinitialized
Attempts:
2 left
💡 Hint
Weak references do not keep the object alive.
🧠 Conceptual
expert
1: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?
AWhen the referenced object will always have a longer lifetime and never be nil during the reference's lifetime.
BWhen you want the reference to automatically become nil when the object is deallocated.
CWhen you want to create a strong reference cycle intentionally.
DWhen the referenced object is a value type like a struct.
Attempts:
2 left
💡 Hint
Think about the difference between weak and unowned references regarding nil values.