---
# System prepended metadata

title: Access Taiwan Lab | iOS Tech Interview Question

---

# Access Taiwan Lab | iOS Tech Interview Question

##  I. Fundamental
### I.I Class vs Struct
1. What is struct and class?

 Answer: 
- Structures are value types, whereas classes are reference types.
- Structures don’t support inheritance, classes do.
- In class, we can create an instance with let keywords and attempt to mutate its property, whereas there is no mutability in structures.
- Structures do not support typecasting, but classes do.

2. What is class only feature that Struct don't have?

Anwser:
- Inheritance: a class can inherit the characteristics of another class.
- Type-casting: helps in checking and interpreting the type of a class instance at runtime.
- Deinitializers: enable an instance of a class to free up any resources it has assigned.
- Reference counting: allows more than one reference to a class instance.

3. When to use struct vs class in Swift?

Anwser:  When creating a new model, we need to carefully contemplate the use cases of the model. Based on this, we should decide between structs and classes.

4. What are value types vs reference types?

Anwser: 

- Value types: Each instance keeps an independent copy of its data, for example — structs, enums or tuples. Changing one instance will have no effect on the other.

- Reference types: Instances share a single copy of the data. Changing data in one instance will change the data for all the instances pointing to the same instance, for example — classes.

### I.II General

5. What are let and var ? What is the difference between them ?

Anwser: Both let and var are for creating variables in Swift. let helps you create immutable variables (constants) while on the other hand var creates mutable variables. Variables created by both of them either hold a reference or a value.

The difference between them is that when you create a constant using let you have to assign something to it before the first use and can’t reassign it. And when you declare a variable with var it can either be assigned right away or at a later time or not at all and can be reassigned at any time.

### I.III Automatic Reference Counting (ARC)

6. What is Automatic Reference Counting (ARC)?

Anwser: Swift uses Automatic Reference Counting (ARC) to track and manage your app’s memory usage. 

7. What is Retain Cycle?

Answer: Retain cycles often cause memory leaks since all of the objects that caused retain cycles cannot be deallocated from memory. 

8. How you solve Retain Cycle?

Answer: In order to prevent this retain cycle, we need to declare at least one of the variable as weak or unowned. We can break the retain cycle with adding a weak keyword before either the driver property of the Car class or the car property of the Person class.


##  II. Practical

### II.I General

1. What is the Value of Variable Len? Why?
```
var arr1 = [1, 2, 3]
var arr2 = arr1
arr2.append(4)
print(arr1.count)
```

2. What is the output for some of the class vs struct below?

Provided Class 1
```
class Book {
    var totalPage: Int?
}
```
Question 1:
```
var book1 = Book()
book1.totalPage = 10
var book2 = book1

print(book1.totalPage!)
print(book2.totalPage!)
```
Question 2:
```
var book1 = Book()
book1.totalPage = 10
var book2 = book1
book2.totalPage = 99

print(book1.totalPage!)
print(book2.totalPage!)
```
---
Provided Class 2
```
class Book {
    var totalPage: Int?
    var pages:[Page] = []
}

class Page {
    var pageNumber: Int?
}
```

Question 1:
```
var book1 = Book()
var book2 = book1
var page = Page()

page.pageNumber = 1
book1.pages.append(page)
book2.pages[0].pageNumber = 12

print(book1.pages[0].pageNumber!)
print(book2.pages[0].pageNumber!)
```
Question 2:
```
var book1 = Book()
var book2 = book1
var page = Page()

page.pageNumber = 1
book1.pages.append(page)

print(book2.pages[0].pageNumber!)
page.pageNumber = 5
print(book2.pages[0].pageNumber!)
```


2. Can you create a Retain Cycle Issue?
Basic:
```
class ViewController: UIViewController {
    override func viewDidLoad() {
            delegate = { [weak self] _ in
                self?.showAlert()
            }
        }
        
    func showAlert() {
        let alert = UIAlertController(title: "No title", message: "Empty message", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { _ in
            alert.dismiss(animated: true, completion: {})
        }))
        
        present(alert, animated: true)
    }
}
```

Advanced:
```
class Car {
  var brand: String
  var driver: Person?
  
  init(brand: String) {
    self.brand = brand
  }
  
  deinit {
    print("Car object is being deallocated")
  }
}
```
```
class Person {
  var name: String
  var car: Car?
  
  init(name: String) {
    self.name = name
  }
  
  deinit {
    print("Person object is being deallocated.")
  }
}
```
```
// Both Car and Person object are initialized
var mercedes: Car? = Car(brand: "mercedes")
var john: Person? = Person(name: "John")

mercedes?.driver = john // Driver of the car is set to John
john?.car = mercedes // Car of the John is set to Mercedes
mercedes = nil // Car object is set to nil
john = nil // Person object is set to nil
```



