# Swift Interview Questions and Answers

Question 12. What are lazy properties?

Answers 12. Lazy properties are properties whose values are not available until there first use. They usually require complex or time taking intialization. lazy properties are declared with lazy keyword.

```class Attendence {
lazy var manager = EmployeeManager()
var maxnumber = 10
}

let attendence  = Attendence()

print("\(attendence.maxnumber)") // manager instance is not created yet

print("\(attendence.manager.presentToday)")  // here manager property has now been created```

Question 13. What are computed properties ?

Answer 13.  Computed properties , unlike stored properties do not store any value. They instead provide getters and setters (which is optional) to get a calculated value or set a stored property after some calculation.

```// example from Apple's swift language guide

struct Point {
var x = 0.0, y = 0.0
}
struct Size {
var width = 0.0, height = 0.0
}
struct Rect {
var origin = Point()
var size = Size()
var center: Point {
get {
let centerX = origin.x + (size.width / 2)
let centerY = origin.y + (size.height / 2)
return Point(x: centerX, y: centerY)
}
set(newCenter) {
origin.x = newCenter.x - (size.width / 2)
origin.y = newCenter.y - (size.height / 2)
}
}
}
var square = Rect(origin: Point(x: 0.0, y: 0.0),
size: Size(width: 10.0, height: 10.0))
let initialSquareCenter = square.center
square.center = Point(x: 15.0, y: 15.0)
print("square.origin is now at (\(square.origin.x), \(square.origin.y))")
// Prints "square.origin is now at (10.0, 10.0)"```

Question 14. Why are Array , String , Dictionaries are value type in swift?

Answer 14.  Arrays, dictionaries etc. in Objective-C are often mutable. That means when we pass an array to another method, and then that array is modified behind the back of the other method, surprising  behavior may occor.

By making arrays, dictionaries etc. value types, this surprising behavior is avoided. When you receive a Swift array, you know that nobody is going to modify it. Objects that can be modified are a major source for problems.

In reality the copying is minimized and occurs only when two references try to have a different value. As the note from apple doc would say

NOTE

The description above refers to the “copying” of strings, arrays, and dictionaries. The behavior you see in your code will always be as if a copy took place. However, Swift only performs an actual copy behind the scenes when it is absolutely necessary to do so. Swift manages all value copying to ensure optimal performance, and you should not avoid assignment to try to preempt this optimization.

A pat on the back !!