Swift Flashcards

1
Q

When to use a For loop or While loop?

A

For loop: when you know the number of iterations ahead of time.

While loop: when you don’t know the number of iterations ahead of time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

== VS ===

A

== tests for the Equatable protocol. ie does “abc” equal “abc”

=== tests for address matching. Do two instances of an object have the same address?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

break vs return

A

Break: will exit loop or switch

Return: will exit the function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What’s the syntax for replacing an element in an array with another element?

A

array[x] = y

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What’s the syntax for assigning a value to a dictionary?

A

dictionary[x] = y

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you check if a dictionary contains a value?

A
  • dict.keys.contains(key)
  • if let value = dict[key]
  • if dict[key] != nil
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you determine how many times you want to repeat an operation?

A

First ask: What data do i need to see? all of it? some of it?

start from base case and develop from there. Consider writing out options.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to iterate both index and value of an array?

A

for (index, value) in array.enumerated()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to iterate elements of a dictionary?

A

for (key, value) in dictionary

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to cast from Int to String?

A

Int(string)

returns optional

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to cast from string to array?

A

Array(string)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How would you instantiate the absolute minimum as an Integer?

A

Int.min

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How would you instantiate the absolute max as an Integer?

A

Int.max

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to return the greater of two values?

A

max(x,y)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to return the lesser of two values?

A

min(x,y)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to return a lowercase version of string?

A

string.lowercased()

this returns a new object!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How would you break a sentence into an array of strings?

A

string.components(seperatedBy: “ “)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How to return the prefix of an array?

A

array.prefix(count)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How to join an array of strings?

A

array.joined(separator: “”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How to remove and return an element in an array?

A

array.remove(at: index)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Are swift parameters passed in as variables or constants?

A

Constants

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What does inout do?

A

Allows you to modify an object in place, instead of returning a new object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What syntax do you use to pass an argument as inout?

A
  • func test(inout string: String)
  • test(string: &string)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How would you insert an element in an array?

A

array.insert(value, at: index)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

How to clean a string of non alpha characters?

A

string.filter { $0.isLetter }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

How to convert a string to an array?

A

let array = Array(string)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What does the type Int32 do?

A

It stores an integer with a length of 32 bits. This is different than the Int type which stores the default system value, which could be 32 or 64.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

How to join an array of strings?

A

let result = array.joined(separator: “”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What is the inout syntax for a func signature?

A

ie func reverseString(string: inout String)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

What is the syntax for getting the first N elements of an array?

A

array.prefix(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

How would you sort an array of this type? [[Int: Int]]

A

array.sort { $0.values.first! < $1.values.first! }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

What is the diff between Sort, Sorted, and Sorted(by: )

A

Sort: called in place
Sorted: returns a copy
Sorted(by: ): returns a copy, accepts > or < as an argument

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

How to swap 2 values?

A

(a, b) = (b, a)

34
Q

What does the Continue keyword do?

A

Prevents a loop from iterating after entering an If block

35
Q

How do you calculate an exponent?

A

let result = pow(x,y)

36
Q

What type does pow(x,y) return and how do you use it?

A

It returns Decimal type and it should be converted to Int/Float by returning:

Int(NSDecimalNumber(decimal: n))

37
Q

How do you pop the last element of an array?

A
  • .removeLast() to remove it
38
Q

What’s the syntax for set?

A

let set: Set<Int> = [1,2,3]</Int>

39
Q

What numbers will this iterate over?

for index in 1…5

A

1,2,3,4,5

40
Q

How many times will this run?

for i in 0..<4

A

4

41
Q

How do you get the length of an array?

A

array.count

42
Q

What is the syntax to perform a swap?

A

(a,b) = (b,a)

43
Q

How can you combine two strings?

A

stringA + stringB

44
Q

How would you iterate an array starting at index N?

A

for i in N…array.count

45
Q

How to create a Set in Swift?

A

let set: Set = [1,2,3]

46
Q

How can you check if a set contains a value?

A

set.contains(value)

47
Q

How to add an element to a Set?

A

set.insert(value)

48
Q

How to declare a function argument as mutable?

A

write inout before the type

func sortColors(_ nums: inout [Int])

49
Q

What is the diff between .reverse() and .reversed() ?

A

.reveresed() returns new value
.reverse() in place

50
Q

What does continue do in a loop?

A

skips an iteration of the loop

51
Q

What does break do in a loop?

A

ends the loop entirely

52
Q

What does Fallthrough do in a loop?

A

allows for a switch case to proceed to the next case

53
Q

How can you create N number of arrays without instantiating them manually?

A

var arrays: [[Int]] = []
let n = 5
for _ in 0..<n {
arrays.append([])
}

54
Q

How can you create N number of arrays without instantiating them manually? Do NOT use a for loop

A

let n = 5
var arrays = [[Int]](repeating: [1,2,3], count: n)

55
Q

How to get absolute number in swift?

A

abs(n)

56
Q

What is the time complexity of .count ?

A

o(1) on an Array or any object that conforms to Random Access Collection
o(n) on non RAC conforming objects

57
Q

How would you iterate an array starting at indexN while also having access to the array’s element?

A

for element in array[1…]

58
Q

Whats the diff between readLine() and CommandLine.arguments?

A
  • readLine() prompts the user for input
  • commandLine pulls in arguments that are passed in at runtime
59
Q

How would you access the last element of an array?

A

array.last

60
Q

How to access strings by index?

A

Convert to array

let stringArray = string.map{ String($0) }

61
Q

Are strings an instance of Array?

A

No, they are an instance of Collection. They are a collection of characters

62
Q

What is the syntax to create a Set of Integers?

A

let set = Set<Int>()</Int>

63
Q

How to access tuple elements?

A

let tuple = (a,b)

let a = tuple.0
let b = tuple.1

64
Q

Continue vs break

A

Continue skips an iteration
break ends the loop

65
Q

How to slice array?

A

array[x…y]

66
Q

How to clear whitespace in Swift?

A

import Foundation

string.trimmingCharacters(in: .whitespaces)

67
Q

What is the expected output after trimming whitespace on this string:

let originalString = “ Remove whitespaces “

A

Output: “Remove whitespaces”

68
Q

How to filter out non-alphanumeric chars?

A

let output = string.filter { $0.isLetter || $0.isNumber }

69
Q

How to get Int from pow() ?

A

let integer = Int(pow(Double(a), Double(b)))

70
Q

How to pop the first element in an array?

A

array.removeFirst()

71
Q

What will 0…5 print?

A

0,1,2,3,4,5

72
Q

What will 0..<5 print?

A

0,1,2,3,4

73
Q

What is the name of this range syntax: 0…5

A

closed

74
Q

What is the name of this range syntax 0..<5

A

half open

75
Q

let nums = [1,2,3]

what does nums[0..<0]) return?

A

An empty array [ ]

76
Q

What numbers does this return?

let array = [1,2,3,4,5]
array[0…3]

A

1,2,3,4

77
Q

What numbers does this return?

let array = [1,2,3,4,5]
array[0..<3]

A

0,1,2,3

78
Q

Is this operator “…” inclusive or exclusive?

ie array[0…array.count]

A

inclusive

79
Q

What will this return?

let array = [1,2,3,4,5]
array[0..<array.count]

A

1,2,3,4,5

80
Q

What will this return?

let array = [1,2,3,4,5]
array[0…array.count]

A

Fatal, index out of range