ARC stands for Automatic Reference Counting. It is a memory management feature in Swift that automatically tracks and manages the app's memory usage.
Click to reveal answer
beginner
Why is ARC important for Swift developers?
ARC helps Swift developers by automatically freeing up memory when objects are no longer needed, preventing memory leaks and crashes without manual intervention.
Click to reveal answer
intermediate
How does ARC know when to free an object’s memory?
ARC keeps track of how many strong references point to an object. When the count drops to zero, meaning no one is using the object, ARC frees its memory.
Click to reveal answer
intermediate
What is a strong reference cycle and why is it a problem in ARC?
A strong reference cycle happens when two objects hold strong references to each other, so their reference counts never reach zero. This causes memory leaks because ARC can’t free them.
Click to reveal answer
intermediate
Name two ways Swift developers can avoid strong reference cycles.
Developers can use weak or unowned references to break strong reference cycles. Weak references don’t increase the reference count and can become nil, while unowned references assume the object always exists during their lifetime.
Click to reveal answer
What triggers ARC to free an object’s memory?
AWhen the object is declared
BWhen the object’s reference count reaches zero
CWhen the app closes
DWhen the object is used for the first time
✗ Incorrect
ARC frees an object’s memory when no strong references point to it, meaning its reference count is zero.
Which of the following can cause a memory leak in Swift?
AUsing unowned references correctly
BUsing weak references
CDeclaring constants
DStrong reference cycle between two objects
✗ Incorrect
Strong reference cycles keep objects alive forever because their reference counts never reach zero, causing memory leaks.
What type of reference does NOT increase an object's reference count?
AWeak reference
BStrong reference
CUnowned reference
DGlobal reference
✗ Incorrect
Weak references do not increase the reference count and can become nil when the object is deallocated.
Which keyword in Swift is used to declare a weak reference?
Aweak
Bstrong
Cunowned
Dvar
✗ Incorrect
The 'weak' keyword declares a weak reference that does not increase the reference count.
What happens if you use an unowned reference and the object it points to is deallocated?
AARC increases the reference count
BThe reference becomes nil
CThe app crashes
DNothing happens
✗ Incorrect
Unowned references assume the object always exists. If the object is deallocated, accessing an unowned reference causes a crash.
Explain in your own words why ARC is important for Swift developers.
Think about how ARC helps manage memory without you having to do it manually.
You got /4 concepts.
Describe what a strong reference cycle is and how you can avoid it in Swift.
Imagine two friends holding hands so tightly they can’t let go — that’s like a strong reference cycle.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of Automatic Reference Counting (ARC) in Swift?
easy
A. To automatically manage memory by tracking object references
B. To optimize the speed of Swift code execution
C. To handle user interface layout automatically
D. To compile Swift code into machine language
Solution
Step 1: Understand ARC's role in memory management
ARC tracks how many references point to an object to manage its memory automatically.
Step 2: Differentiate ARC from other tasks
ARC does not optimize speed, handle UI layout, or compile code; it focuses on memory.
Final Answer:
To automatically manage memory by tracking object references -> Option A
Quick Check:
ARC = memory management [OK]
Hint: ARC counts references to manage memory automatically [OK]
Common Mistakes:
Confusing ARC with code compilation
Thinking ARC manages UI layout
Assuming ARC improves execution speed
2. Which of the following is the correct way to declare a weak reference in Swift?
easy
A. var delegate: weak MyDelegate?
B. weak var delegate: MyDelegate?
C. var weak delegate: MyDelegate?
D. var delegate: MyDelegate weak?
Solution
Step 1: Recall Swift syntax for weak references
In Swift, the keyword weak comes before the variable declaration type.
Step 2: Check each option's syntax
weak var delegate: MyDelegate? correctly places weak before var and the type, matching Swift syntax.
Final Answer:
weak var delegate: MyDelegate? -> Option B
Quick Check:
Weak reference syntax = "weak var" [OK]
Hint: Use 'weak var' before the variable name for weak references [OK]
Common Mistakes:
Placing 'weak' after the variable name
Using 'var weak' instead of 'weak var'
Omitting the optional '?' for weak references
3. Consider the following Swift code:
class Person {
var name: String
init(name: String) {
self.name = name
}
}
var person1: Person? = Person(name: "Alice")
var person2 = person1
person1 = nil
print(person2?.name ?? "No name")
What will be printed?
medium
A. Runtime error
B. "No name"
C. nil
D. "Alice"
Solution
Step 1: Understand reference counting in the code
Both person1 and person2 point to the same Person object initially, so reference count is 2.
Step 2: Analyze effect of setting person1 to nil
Setting person1 to nil reduces reference count to 1, but person2 still holds the object.
Step 3: Determine print output
Since person2 still references the object, person2?.name is "Alice".
Final Answer:
"Alice" -> Option D
Quick Check:
Reference count > 0 means object exists [OK]
Hint: Object stays alive if any strong reference exists [OK]
Common Mistakes:
Assuming object is deallocated when one reference is nil
Confusing optional chaining output
Expecting runtime error due to nil assignment
4. Given this Swift code snippet, what is the main problem?
class Node {
var value: Int
var next: Node?
init(value: Int) {
self.value = value
}
}
var first: Node? = Node(value: 1)
var second: Node? = Node(value: 2)
first?.next = second
second?.next = first
medium
A. Syntax error in class definition
B. Runtime crash due to nil unwrapping
C. Strong reference cycle causing memory leak
D. No problem, code runs fine
Solution
Step 1: Identify references between nodes
first points to second, and second points back to first, creating a cycle.
Step 2: Understand ARC impact of strong cycles
Both nodes hold strong references to each other, so ARC cannot deallocate them, causing a memory leak.
Final Answer:
Strong reference cycle causing memory leak -> Option C
Quick Check:
Strong cycles = memory leaks [OK]
Hint: Check for mutual strong references causing leaks [OK]
Common Mistakes:
Thinking this causes syntax errors
Assuming ARC handles cycles automatically
Ignoring possibility of memory leaks
5. You have two classes, Teacher and Student, where each Teacher has a list of Student objects, and each Student has a reference to their Teacher. How should you declare these references to avoid memory leaks caused by ARC?
hard
A. Declare Teacher's students as strong and Student's teacher as weak
B. Declare both references as strong
C. Declare Teacher's students as weak and Student's teacher as strong
D. Declare both references as weak
Solution
Step 1: Identify ownership direction
The Teacher owns the list of Student objects, so this should be a strong reference.
Step 2: Avoid strong cycle by making reverse reference weak
The Student references back to Teacher should be weak to prevent a strong reference cycle.
Final Answer:
Declare Teacher's students as strong and Student's teacher as weak -> Option A
Quick Check:
Strong ownership one way, weak back reference [OK]
Hint: Make back references weak to break strong cycles [OK]
Common Mistakes:
Making both references strong causing leaks
Making both references weak causing premature deallocation
Confusing which side should be weak
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.