Part 1 — iOS Interview Preparation Questions and Explanations
By: Mohd Yasir
Tags: iOS, Swift, Interview, Programming, Interview Questions

100 + Que/Ans Series , I will post a continue series on iOS Interview questions from a basic concept to advance .

Quick Note : If you want to be updated of this series , go through this link or check comment section of this Article . “You will get answer for given question in first few line , and after that there will be some insights and key points about that question .”

1. What is Type Inference ?

In short its an ability of swift . You dont always need to write types of variables and constant you making in your code . For example :

// swift know its Int type

var age = 40 // Int

// You dont need to tell them always like below

var age : Int = 40

2. What is Generics ?

Generic code enables you to write flexible, reusable functions and types that can work with any type, subject to requirements that you define.

Understanding it with an example :

Suppose you want to swap to values of type Int , lets write a non-generic functions :

func swapTwoInts(_ a: inout Int, _ b: inout Int) {

let temporaryA = a

a = b

b = temporaryA

} var num1 = 4

var num2 = 5

swapTwoInts(&num1 , &num2)

Now , suppose you want to swap two double values or two string values , you will need to write another function for that , because the above function is accepting only for Int type .

What if we have a function which accept any type of values and swap them , this is what generic do .

Now lets do the same thing with a generic function :

func swapTwoValues<T>(_ a: inout T, _ b: inout T) {

let temporaryA = a

a = b

b = temporaryA

}

var num1 = 3

var num2 = 4

swapTwoValues(&num1 , &num2) var str1 = "sdf"

var str2 = "dafdf"

swapTwoValues(&str1 , &str2)

Now you can swap any type of values , you dont need to write different different function to swap different different type of values.

T is a placeholder , called as type parameter .

We use array in swift which is also a generic type

Array <Element> , Dictionary<Key , Value>

3. What are Protocols ?

Its a blueprint of methods, properties, and other requirements that suit a particular task and it could adopted by a class , structure or enumeration .

Protocol does not include any implementation !!!! Type which is adopting the protocol , should have all the methods which are present in given protocol . And this action is called conforming protocol .

Its syntax looks like :

protocol Vehicle {

func accelerate()

func stop()

} class Unicycle : Vehicle {

var peddling = false

func accelerate(){

peddling = true

}

func stop() {

peddling = false

}

}

4. What are Tuples ?

Sometimes data comes in pairs or triplets. An example of this is a pair of (x, y) coordinates on a 2D grid. Similarly, a set of coordinates on a 3D grid is comprised of an x-value, a y-value and a z-value. In Swift, you can represent such related data in a very simple way through the use of a tuple.

let coordinates: (Int, Int) = (2, 3)

5. What about Mutability in Swift ?

Constant(let) are constant in swift and variable(var) varies .

6. What are Subscripts ?

With subscripts you can quickly access the member elements of collections.

A subscript consists of:

The name of the collection, such as scores

Two square brackets [ and ]

and A key or index inside the brackets

By default, you can use subscripts with arrays, dictionaries, collections, lists and sequences. You can also implement your own with the subscript function.

subscript(parameterList) -> ReturnType {

get {

// return someValue of ReturnType

}

set(newValue) {

// set someValue of ReturnType to newValue

}

7. What is an Optional ?

Optionals are Swift’s solution to the problem of representing both a value and the absence of a value. An optional is allowed to hold either a value or nil.

8. In what ways you could Unwrap an optional ?

We can unwrap any optional in following ways :

By Optional Binding By Force Unwrapping By Guard Statement By Nil Coalescing

Optional Binding (If let)

Its simplest way to do unwrap an optional .

var authorName : String? = "Mohd Yasir" if let authorName == authorName {

print("Author name is \(authorName)")

else{

print("No Author Name")

}

By Force Unwrapping

To force unwrap , we use “!” .

var authorName : String? = "Mohd Yasir"

print("Auhor name : \(authorName!)")

Guard Statement

Sometimes you want to check a condition and only continue executing a function if the condition is true, such as when you use optionals. Imagine a function that fetches some data from the network. That fetch might fail if the network is down. The usual way to encapsulate this behavior is using an optional, which has a value if the fetch succeeds, and nil otherwise.

Swift has a useful and powerful feature to help in situations like this: the guard statement.

func testingGuard( _ name : String?){

guard let unrappedname = name else {

print("You dont entered any name")

return

}

print("Hello , \(unrappedname)")

}

Nil Coalescing

let name = String? = nil

let unwrappedName = name5 ?? "Unkonwn"

9. What kind of memory allocations takes place in Swift ?

In short Stack and Heap

When you create a reference type such as class, the system stores the actual instance in a region of memory known as the heap. Instances of a value type such as a struct resides in a region of memory called the stack .

10. What is the difference between stack and heap memory ?