Sample questions
Question 1Easy
How does ARC (Automatic Reference Counting) decide when to free a class instance?
- When the number of strong references to it drops to zero
- When a periodic garbage collector finds it unreachable
- When the app moves into the background
- When the function that created it returns
Answer: A. When the number of strong references to it drops to zero
Explanation
The compiler inserts retain and release calls. The instance is deallocated as soon as its strong reference count reaches zero; there is no tracing garbage collector.
Step-by-step solution
- ARC adds retain when a strong reference is created.
- ARC adds release when a strong reference is removed.
- Instance is freed when all strong references are gone.
- This happens when the last strong reference is released.
- Final answer: When the number of strong references to it drops to zero.
Why the other options are wrong
- B. Swift does not use a periodic garbage collector; ARC frees objects at once when counts reach zero.
- C. Moving to the background does not free objects; ARC counts references, not app state.
- D. Returning from a function does not free an instance; strong references decide.
Tip: Remember, ARC frees objects when all strong references are gone.
Question 2Easy
- A weak reference that keeps pointing at a freed object
- An object retained twice by the same variable
- Objects strongly holding each other so neither is freed
- A loop that allocates objects faster than ARC can free them
Answer: C. Objects strongly holding each other so neither is freed
Explanation
If A strongly holds B and B strongly holds A, their counts never reach zero, so both leak even when nothing else refers to them.
Step-by-step solution
- A retains B because it strongly holds it.
- B retains A because it strongly holds it.
- Neither can be freed as their counts never reach zero.
- This causes both objects to leak memory.
- Final answer: Objects strongly holding each other so neither is freed.
Why the other options are wrong
- A. Weak references become nil when the object is freed, so they do not form a cycle.
- B. Being retained twice by one variable is not a cycle; a cycle needs objects holding each other.
- D. Fast allocation is not a retain cycle; the cycle is about mutual strong references.
Tip: Retain cycles are a common cause of memory leaks in object-oriented programming.
Question 3Easy
What is the output?
class A { deinit { print("bye") } }
var a: A? = A()
print("start")
a = nil
print("end")
- start bye end
- start end
- bye start end
- start end bye
Answer: A. start bye end
Explanation
Setting the only strong reference to nil drops the count to zero, so deinit runs straight away, before "end" is printed.
Step-by-step solution
- A holds the only strong reference to the A object.
- print('start') runs first.
- a = nil removes the last strong reference, so deinit runs and prints bye before end.
- Output is start bye end, so the answer is option A.
Why the other options are wrong
- B. Deinit runs when a is set to nil, so bye is printed.
- C. Bye is printed after start, not before it.
- D. Bye is printed during a = nil, before end.
Tip: Setting the last strong reference to nil frees the object at once.
Question 4Easy
Which declaration of a weak reference is valid?
- weak var parent: Node?
- weak var parent: Node
- weak let parent: Node
- weak let parent: Node?
Answer: A. weak var parent: Node?
Explanation
A weak reference must be a var of optional type, because ARC sets it to nil when the object is deallocated.
Step-by-step solution
- A weak reference must be var, because it is set to nil later.
- A weak reference must be optional, since it can become nil.
- Weak var parent: Node? meets both rules.
- So option A is the valid declaration.
Why the other options are wrong
- B. Weak needs an optional type, so Node without? is invalid.
- C. Weak cannot be used with let.
- D. Weak let is invalid; weak must be var.
Tip: A weak reference is always var and always optional.
Question 5Medium
A class stores a closure in a property, and that closure calls self.update(). What problem can this cause, and what is the usual fix?
- A retain cycle; fix it with [weak self]
- A crash on first call; mark the property lazy
- A data race; mark the closure @MainActor
- A compile error; mark the closure @escaping
Answer: A. A retain cycle; fix it with [weak self]
Explanation
The object holds the closure and the closure holds the object strongly, so neither is freed. [weak self] (then guard let self) breaks the cycle.
Step-by-step solution
- The closure holds the object strongly.
- The object holds the closure strongly.
- This creates a retain cycle.
- Use [weak self] to break the cycle.
- Final answer: A retain cycle; fix it with [weak self]
Why the other options are wrong
- B. A lazy property does not crash on first call; the problem here is a strong cycle through the closure.
- C. No data race here; the problem is a strong reference cycle, not concurrency.
- D. Missing @escaping is a compile error, not a runtime leak, so marking it does not fix the cycle.
Tip: Always use [weak self] in closures that reference self.
Question 6Medium
What is the output?
class A {}
var a: A? = A()
weak var w = a
a = nil
print(w == nil)
- nil
- false
- Runtime crash: dangling reference
- true
Answer: D. true
Explanation
w is weak, so it doesn't keep the object alive. Once a is set to nil the only strong reference is gone, the object is freed and w becomes nil.
Step-by-step solution
- A holds the only strong reference to the object.
- Weak var w = a creates a weak reference to that object.
- a = nil removes the last strong reference, so the object is freed and w becomes nil.
- print(w == nil) prints true, so the answer is option D.
Why the other options are wrong
- A. The printed value is a Bool, so it prints true, not nil.
- B. After the object is freed, w is nil, so the result is true, not false.
- C. A weak reference becomes nil safely; there is no crash.
Tip: Weak references become nil automatically when the object is freed.
Question 7Medium
How does an unowned reference differ from a weak one?
- unowned is non-optional and assumes the object outlives it
- unowned becomes nil automatically when the object is freed
- unowned can only be used with structs, weak only with classes
- unowned keeps the object alive, while weak does not
Answer: A. unowned is non-optional and assumes the object outlives it
Explanation
Neither increases the retain count. weak is optional and is set to nil on deallocation. unowned is non-optional, and using it after the object is freed crashes.
Step-by-step solution
- Unowned is non-optional, so the reference is never nil.
- The programmer must guarantee that the object lives as long as the unowned reference is used.
- If the object is already freed, using the unowned reference crashes.
- So option A is correct.
Why the other options are wrong
- B. Unowned does not become nil; weak does.
- C. Unowned works with classes; it is not limited to structs.
- D. Neither unowned nor weak keeps the object alive; both are non-owning references.
Tip: Use weak when the reference may become nil; use unowned only when the owner lives at least as long.
Question 8Medium
Why is a delegate property usually declared weak, as in weak var delegate: SomeDelegate?
- The delegate often owns the object, so it would form a cycle
- Strong references cannot point at objects that adopt protocols
- Delegates must be optional, and only weak properties can be optional
- weak references make delegate method calls faster
Answer: A. The delegate often owns the object, so it would form a cycle
Explanation
A view controller typically owns a helper and is also its delegate. If the helper held the delegate strongly, the two would keep each other alive.
Step-by-step solution
- The object that has a delegate is often owned by its delegate, for example a view controller that holds a helper object.
- If the helper also holds its delegate strongly, each one keeps the other alive.
- Weak breaks the cycle, so the reason is option A.
Why the other options are wrong
- B. Objects that adopt protocols can be held strongly or weakly.
- C. Being optional is a rule for weak variables, not the reason delegates are weak.
- D. Weak is about memory ownership, not speed.
Tip: Make delegate references weak to avoid retain cycles.
Question 9Hard
A view controller starts DispatchQueue.main.asyncAfter(deadline: .now() + 10) { self.reload() } and is then dismissed after 1 second. What happens?
- It is freed at once and the closure crashes on self
- The closure is cancelled automatically when it is dismissed
- It leaks for ever because of a permanent retain cycle
- It stays alive until the closure runs, then can be freed
Answer: D. It stays alive until the closure runs, then can be freed
Explanation
The queue holds the closure, which holds self, so deallocation is delayed by ten seconds. It's not a cycle, because self doesn't hold the closure. [weak self] would let it be freed straight away.
Step-by-step solution
- The closure is scheduled on the main queue after 10 seconds.
- The closure holds a strong reference to self, preventing deallocation.
- The view controller is dismissed after 1 second but still holds the closure.
- The closure runs after 10 seconds, then releases self, allowing it to be freed.
- The closure stays alive until it runs, then can be freed.
Why the other options are wrong
- A. The object is not freed at once; the closure holds self strongly, so it survives until the closure runs.
- B. Dispatch closures are not cancelled when a view controller is dismissed; they keep running.
- C. Not a permanent leak: the closure runs after 10 seconds and releases self, so the cycle ends.
Tip: Always use [weak self] in closures to avoid retain cycles.
Question 10Hard
Which kinds of values does ARC actually reference-count?
- Class instances and closure contexts
- Only objects created with Objective-C APIs
- Only structs stored in arrays
- Every value, including Int and Bool
Answer: A. Class instances and closure contexts
Explanation
Reference counting applies to heap objects with shared identity: class instances and closure contexts. Plain value types like Int, or structs of them, are copied rather than counted (though a struct holding class references retains them).
Step-by-step solution
- ARC reference-counts class instances because they are heap-allocated and shared.
- Closures capture contexts which are also heap-allocated and counted by ARC.
- Int and Bool are value types copied, not retained or released by ARC.
- Structs of value types are copied, so ARC does not count them.
- So the answer is class instances and closure contexts; value types themselves are copied, not counted.
Why the other options are wrong
- B. ARC is not limited to Objective-C objects; it counts Swift class instances too.
- C. Structs are value types and are copied, so ARC does not count them, even in arrays.
- D. Int and Bool are value types, so ARC does not count them; they are copied.
Tip: Remember: ARC counts class instances and closure contexts; value types are copied, not counted.
Practise all 13 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →