Practice 50 Swift Memory Management Programming Questions
Q1. Write a Swift program to demonstrate the use of automatic reference counting (ARC) by creating a class with a property and showing the memory management behavior.
Input:
Property = "Hello World"
Expected Output:
ARC is managing the memory.
Q2. Write a Swift program to create a class Person and demonstrate the effect of strong references by creating an instance and showing memory behavior.
Input:
name = "John Doe"
Expected Output:
Strong reference causes memory to persist until explicitly deallocated.
Q3. Write a Swift program to create a class Book with a method deinit() to show how deinitializers are used to release resources when an object is deallocated.
Input:
Book = "Swift Programming"
Expected Output:
Book instance deallocated.
Q4. Write a Swift program to demonstrate the concept of weak references by creating a class with weak properties and observing the memory management behavior.
Input:
person = Person(name: "Alice")
Expected Output:
Weak reference does not increase the reference count.
Q5. Write a Swift program to demonstrate the use of unowned references by creating a strong reference cycle and using unowned to break the cycle.
Input:
person = Person(name: "Bob")
Expected Output:
Unowned reference prevents strong reference cycle.
Q6. Write a Swift program to create a class Car with a deinitializer that releases resources when the object is deallocated.
Input:
car = "BMW"
Expected Output:
Car object deallocated.
Q7. Write a Swift program to show how retain cycles occur by creating a scenario where two objects retain each other.
Input:
person = Person(name: "Charlie")
Expected Output:
Retain cycle occurs and objects are not deallocated.
Q8. Write a Swift program to demonstrate how to resolve retain cycles using weak references.
Input:
parent = Parent(name: "Daniel")
Expected Output:
Retain cycle resolved with weak reference.
Q9. Write a Swift program to demonstrate the deinit method by creating a class and observing when it is called during the deallocation of an object.
Input:
object = "Example"
Expected Output:
deinit method is called for Example.
Q10. Write a Swift program to create a Student class with a method deinit() that prints a message when the object is deallocated.
Input:
student = "Mark"
Expected Output:
Student object Mark is deallocated.
Q11. Write a Swift program to demonstrate the effect of strong and weak references in a closure.
Input:
closure = { print("Hello") }
Expected Output:
Strong reference inside closure causes memory retention.
Q12. Write a Swift program to create a class Library with a weak reference to Book, and demonstrate memory management when the Book object is deallocated.
Input:
library = Library(), book = Book(title: "iOS Programming")
Expected Output:
Book instance is deallocated when no longer referenced by any strong references.
Q13. Write a Swift program to demonstrate how to use weak references to prevent memory leaks in a parent-child relationship.
Input:
parent = Parent(), child = Child()
Expected Output:
Memory leak prevented using weak reference.
Q14. Write a Swift program to demonstrate how unowned references prevent memory leaks in a closure capturing a reference to an object.
Input:
closure = { [unowned self] in self.doSomething() }
Expected Output:
Unowned reference prevents strong reference cycle in closure.
Q15. Write a Swift program to show how ARC manages memory by showing the reference count of an object before and after strong, weak, and unowned references are set.
Input:
object = SomeObject()
Expected Output:
Reference count before weak reference: 1
Reference count after weak reference: 0
Q16. Write a Swift program to create a class with multiple strong references and observe how memory is managed when one reference goes out of scope.
Input:
object = SomeClass()
Expected Output:
Object memory is released when the last strong reference is deallocated.
Q17. Write a Swift program to demonstrate how circular references can be avoided in closures.
Input:
closure = { [weak self] in self?.performAction() }
Expected Output:
No memory leak due to weak self in closure.
Q18. Write a Swift program to create a class User with a method logout() that is called when an object is deallocated.
Input:
user = "John"
Expected Output:
User John has logged out and the object is deallocated.
Q19. Write a Swift program to show how memory is managed with closures capturing reference types and value types differently.
Input:
closure = { [weak self] in self?.updateUI() }
Expected Output:
Captured reference to value type doesn’t cause retain cycle.
Q20. Write a Swift program to demonstrate the use of deinit with a class and how ARC cleans up memory when the object goes out of scope.
Input:
object = "Resource"
Expected Output:
deinit method is called and resources are cleaned up.
Q21. Write a Swift program to show how unowned references are used in closures to prevent memory leaks.
Input:
closure = { [unowned self] in self.doWork() }
Expected Output:
No memory leak due to unowned reference in closure.
Q22. Write a Swift program to demonstrate how objects with strong references are deallocated when they go out of scope.
Input:
object = "ObjectName"
Expected Output:
ObjectName is deallocated when it goes out of scope.
Q23. Write a Swift program to create a class Employee and show how to properly use weak references to prevent strong reference cycles in the class.
Input:
employee = Employee(name: "Alice")
Expected Output:
Memory leak prevented using weak reference.
Q24. Write a Swift program to demonstrate the difference between strong and weak references in ARC.
Input:
strongRef = object, weakRef = object
Expected Output:
Strong reference retains memory, weak reference does not.
Q25. Write a Swift program to create a class Person and demonstrate how to properly manage the deallocation using deinit method.
Input:
person = "David"
Expected Output:
David is deallocated successfully.
Q26. Write a Swift program to show how memory is freed automatically when the object goes out of scope, using the deinit method.
Input:
object = "SomeObject"
Expected Output:
Memory is freed when object goes out of scope.
Q27. Write a Swift program to demonstrate the use of weak references when capturing self in closures.
Input:
closure = { [weak self] in self?.executeTask() }
Expected Output:
No retain cycle due to weak self in closure.
Q28. Write a Swift program to create a class Task with a method deinit() that shows the release of resources when the object is deallocated.
Input:
task = Task()
Expected Output:
Task instance deallocated and resources freed.
Q29. Write a Swift program to demonstrate the memory management behavior when using reference types in closures.
Input:
closure = { obj in obj?.performAction() }
Expected Output:
Reference type object is captured and retained by closure.
Q30. Write a Swift program to demonstrate the behavior of ARC with strong references in a cyclic dependency between two classes.
Input:
classA = ClassA(), classB = ClassB()
Expected Output:
Memory leak occurs due to cyclic reference.
Q31. Write a Swift program to create a class with a strong reference cycle and show how ARC handles memory management.
Input:
personA = PersonA(), personB = PersonB()
Expected Output:
Strong reference cycle detected and memory is not released.
Q32. Write a Swift program to create a class with a deinit method and show when memory is deallocated.
Input:
instance = "MyObject"
Expected Output:
Memory deallocated and deinit method called.
Q33. Write a Swift program to demonstrate how weak references prevent retain cycles in a parent-child relationship.
Input:
parent = Parent(), child = Child()
Expected Output:
Weak reference prevents retain cycle between parent and child.
Q34. Write a Swift program to create a class with a strong reference and weak reference, and print the reference count before and after deallocation.
Input:
object = SomeObject()
Expected Output:
Reference count before deallocation: 1
Reference count after deallocation: 0
Q35. Write a Swift program to demonstrate the use of ARC in a scenario where objects are deallocated after losing all strong references.
Input:
object = "SomeClass"
Expected Output:
Object is deallocated when no strong references exist.
Q36. Write a Swift program to show how to fix a retain cycle in closures by using weak or unowned references.
Input:
closure = { [weak self] in self?.method() }
Expected Output:
Retain cycle fixed with weak reference.
Q37. Write a Swift program to create an object that automatically deallocates when the reference count drops to zero.
Input:
object = "AutoDeallocate"
Expected Output:
Object is deallocated when reference count is zero.
Q38. Write a Swift program to create a TaskManager class with a deinit() method that prints when the task manager is deallocated.
Input:
taskManager = TaskManager()
Expected Output:
TaskManager is deallocated.
Q39. Write a Swift program to create a class with a strong reference and simulate memory deallocation with weak references.
Input:
object = "TestObject"
Expected Output:
Memory is deallocated using weak reference.
Q40. Write a Swift program to show how weak references allow deallocation when no longer needed.
Input:
object = "ExampleObject"
Expected Output:
ExampleObject is deallocated using weak reference.
Q41. Write a Swift program to create an instance with strong and weak references, and show when each reference is deallocated.
Input:
strong = StrongObject(), weak = WeakObject()
Expected Output:
Strong reference retains, weak reference allows deallocation.
Q42. Write a Swift program to create a class with an unowned reference and demonstrate memory behavior when the object goes out of scope.
Input:
object = UnownedObject()
Expected Output:
Unowned reference prevents memory leak.
Q43. Write a Swift program to demonstrate the difference between weak and unowned references.
Input:
weakRef = SomeObject(), unownedRef = SomeObject()
Expected Output:
Weak reference can be nil, unowned cannot be nil.
Q44. Write a Swift program to demonstrate how to manage memory with ARC in a multi-threaded environment.
Input:
object = "MultiThreadedObject"
Expected Output:
Memory management handled by ARC across threads.
Q45. Write a Swift program to show how to prevent memory leaks by using weak references in a collection of objects.
Input:
collection = [SomeObject(), AnotherObject()]
Expected Output:
Memory leaks prevented by weak references in collection.
Q46. Write a Swift program to create a class that tracks reference counts and shows the current count before and after strong, weak, and unowned references are set.
Input:
object = "ReferenceObject"
Expected Output:
Reference count before weak reference: 1
Reference count after weak reference: 0
Q47. Write a Swift program to show that weak references do not retain objects in closures.
Input:
closure = { [weak self] in self?.doSomething() }
Expected Output:
Weak reference does not retain object inside closure.
Q48. Write a Swift program to demonstrate a retain cycle scenario with strong references and how weak references resolve it.
Input:
classA = ClassA(), classB = ClassB()
Expected Output:
Memory leak prevented by weak reference.
Q49. Write a Swift program to show how ARC works with strong, weak, and unowned references in a class with circular dependencies.
Input:
classA = ClassA(), classB = ClassB()
Expected Output:
Retain cycle avoided by weak reference.
Q50. Write a Swift program to demonstrate how weak and unowned references allow for proper memory deallocation in a strongly referenced cycle.
Input:
classA = ClassA(), classB = ClassB()
Expected Output:
Memory properly deallocated using weak or unowned references.
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!