Skip to content

Instantly share code, notes, and snippets.

@cooler333
Forked from boska/ios-interview.md
Created January 5, 2018 13:00
Show Gist options
  • Save cooler333/f051fa890363949c866947f1d8c7a223 to your computer and use it in GitHub Desktop.
Save cooler333/f051fa890363949c866947f1d8c7a223 to your computer and use it in GitHub Desktop.
ios-interview.md

1.

Consider the following code

var array1 = [1, 2, 3, 4, 5]
var array2 = array1
array2.append(6)
var len = array1.count

What’s the value of the len variable, and why?

2.

Consider the following code

let op1: Int = 1
let op2: UInt = 2
let op3: Double = 3.34
var result = op1 + op2 + op3

Where is the error and why? How can it be fixed?

3.

Consider the following code

var defaults = UserDefaults.standard
var userPref = defaults.string(forKey: "userPref")
func printString(string: String) {
  print(string)
}
printString(string: userPref)

Where is the bug? What does this bug cause? What’s the proper way to fix it?

4.

In Swift enumerations, what’s the difference between raw values and associated values?

5.

Consider the following code

struct IntStack {
  var items = [Int]()
  func add(x: Int) {
    items.append(x) // Compile time error here.
  }
}

Explain why a compile time error occurs. How can you fix it?

6.

What's the differnce on these access control keywords

open
public
internal
fileprivate
private

7.

What will this code print out and why?

import UIKit
var thing = "cars"

let closure = { [thing] in
  print("I love \(thing)")
}

thing = "airplanes"

closure()

8.

The following code has a compile time error. Can you spot where and why it happens?

struct Kitten {
}

func showKitten(kitten: Kitten?) {
  guard let k = kitten else {
    print("There is no kitten")
  }
    
  print(k)
}

9.

What are the various ways to unwrap an optional?

10.

What's the extension in Swift? Explain.

11.

The following code has a compile time error. Can you spot where and why it happens?

protocol Movable {
    var x: Int { set }
    var y: Int { get set }
    func move(x: Int, y: Int)
}

12.

Consider the following code

func areEqual(x: Int, y: Int) -> Bool {
  return x == y
}

func areEqual(x: String, y: String) -> Bool {
  return x == y
}

Please refactory these two functions with generics into one function

13.

Please explain these Higher-order functions in Swift

map
filter
reduce
flatmap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment