Bird
Raised Fist0
Swiftprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does ARC stand for in Swift's memory management?
easy
A. Active Reference Control
B. Automatic Resource Control
C. Asynchronous Runtime Code
D. Automatic Reference Counting

Solution

  1. Step 1: Understand the acronym ARC

    ARC in Swift stands for Automatic Reference Counting, which manages memory automatically.
  2. Step 2: Eliminate incorrect options

    Other options do not match the correct term used in Swift memory management.
  3. Final Answer:

    Automatic Reference Counting -> Option D
  4. Quick Check:

    ARC = Automatic Reference Counting [OK]
Hint: Remember ARC counts references automatically [OK]
Common Mistakes:
  • Confusing ARC with manual memory management
  • Thinking ARC stands for resource control
  • Mixing ARC with asynchronous code concepts
2. Which of the following is the correct way to declare a class in Swift that ARC will manage?
easy
A. struct Person {}
B. class Person {}
C. func Person() {}
D. var Person = {}

Solution

  1. Step 1: Identify ARC-managed types

    ARC manages memory for class instances, so classes are declared with class.
  2. Step 2: Check syntax correctness

    class Person {} is the correct syntax for a class in Swift.
  3. Final Answer:

    class Person {} -> Option B
  4. Quick Check:

    Class declaration = class Person {} [OK]
Hint: ARC manages classes, not structs or functions [OK]
Common Mistakes:
  • Using struct instead of class for ARC examples
  • Trying to declare a class with func or var
  • Confusing function and class syntax
3. Consider the following Swift code:
class Dog {}
var dog1: Dog? = Dog()
var dog2 = dog1
 dog1 = nil
print(dog2 == nil)

What will be printed?
medium
A. true
B. Compile error
C. false
D. nil

Solution

  1. Step 1: Analyze reference assignments

    dog1 is assigned a Dog instance, dog2 copies the reference from dog1, so both point to the same object.
  2. Step 2: Effect of setting dog1 to nil

    Setting dog1 to nil removes one reference, but dog2 still holds the reference, so the object remains alive.
  3. Step 3: Evaluate print statement

    dog2 is not nil, so dog2 == nil is false, so it prints false.
  4. Final Answer:

    false -> Option C
  5. Quick Check:

    dog2 still references object = false [OK]
Hint: Setting one reference nil doesn't free object if others exist [OK]
Common Mistakes:
  • Assuming dog2 becomes nil when dog1 is nil
  • Confusing reference assignment with copying object
  • Expecting a compile error due to optional usage
4. What is the problem with this Swift code snippet related to ARC?
class Person {
  var friend: Person?
}

var alice: Person? = Person()
alice?.friend = alice
alice = nil
medium
A. Memory leak due to strong reference cycle
B. Compile error because of optional chaining
C. No problem, memory is freed correctly
D. Runtime crash due to nil assignment

Solution

  1. Step 1: Identify reference cycle

    alice references a Person instance, and that instance's friend property points back to alice, creating a strong cycle.
  2. Step 2: Effect on ARC

    Because both references are strong, ARC cannot free the Person instance even after alice is set to nil, causing a memory leak.
  3. Final Answer:

    Memory leak due to strong reference cycle -> Option A
  4. Quick Check:

    Strong cycle prevents deallocation = memory leak [OK]
Hint: Strong references pointing to each other cause leaks [OK]
Common Mistakes:
  • Thinking optional chaining causes compile error
  • Assuming ARC frees memory despite cycles
  • Believing nil assignment crashes program
5. You want to avoid a strong reference cycle between two classes Owner and Pet. Which is the best way to declare the reference from Pet to Owner to prevent memory leaks?
hard
A. Declare the reference as weak var owner: Owner?
B. Declare the reference as var owner: Owner
C. Declare the reference as unowned var owner: Owner?
D. Declare the reference as let owner: Owner

Solution

  1. Step 1: Understand strong vs weak references

    Strong references increase ARC count and can cause cycles; weak references do not increase ARC count.
  2. Step 2: Choose weak reference to break cycle

    Declaring owner as weak var owner: Owner? allows ARC to free objects properly by not increasing reference count.
  3. Final Answer:

    Declare the reference as weak var owner: Owner? -> Option A
  4. Quick Check:

    Use weak to avoid strong reference cycles [OK]
Hint: Use weak references to break cycles safely [OK]
Common Mistakes:
  • Using unowned incorrectly causing crashes
  • Using strong references causing memory leaks
  • Forgetting to make weak references optional
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