Fundamentals Flashcards

1
Q

Keyword to create an enumeration Type?

A

enum

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

What type of syntax do you use to access enums?

A

dot syntax

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

Can you use an enum in a switch statement?

A

Yes

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

How do you declare an enum with raw values?

A

enum ENUM_NAME: String {
case ENUM_CASE1 = “VALUE1”
case ENUM_CASE2 = “VALUE2”
}

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

How do you declare an enum with associated values?

A
enum ENUM_NAME {
  case ENUM_CASE1(CASETYPE1)
  case ENUM_CASE2(CASETYPE2)
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you access the value associated with an enum case in a switch statement?

A

case .ENUM_CASE(let VALUENAME):

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

What do you call variables within a Struct, Class?

A

Properties of a Struct, Class

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

Keyword to create different enumeration choices?

A

case

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

Do you have to write initializers for Structs?

A

No. Swift will automatically create a “memberwise initializer”

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

Do all properties of a struct need to be declared when instantiating an instance of a structure?

A

Yes

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

What do you call the object created from a Struct/Class?

A

Instance of a Struct/Class

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

Are dictionaries ordered or unordered?

A

Unordered

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

How do you access the enum’s raw value?

A

.rawValue getter

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

When you declare a variable as an enum type and you are setting it, can you use a shorter syntax?

A

Yes, use dot syntax. (exp: .caseItem)

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

How do you declare and initialize a dictionary using a literal format?

A

var DICT = [
“KEY1”: “VALUE1”,
“KEY2”: “VALUE2”
]

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

What do you call functions within a Struct, Class?

A

Methods of a Struct, Class

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

Are “switch case” statements labels capitalized?

A

No

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

How do you force unwrap optionals?

A

Using the bang (!) symbol after a variable name

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

When you set the value of a dictionary key to nil, does it remove it from the collection?

A

Yes

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

How do you declare and initialize an empty dictionary of type String: Int

A

var DICT: [String: Int] = [:]

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

What method can you call that returns a tuple containing an index and the value?

A

array.enumerated()

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

When Iterating a dictionary, could you get a different order of items?

A

Yes

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

When getting the value of a dictionary does it return an optional value?

A

Yes

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

What keyword do you use to define a tuple?

A

Parenthesis (ITEM1, ITEM2)

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

When a function returns a tuple can you define the labels associated with it?

A
Yes
func MYFUNC() -> (LABEL1: String, LABEL2: Int) { }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

When creating an enum is it a Type?

A

Yes

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

Can you nest Enums within a Struct?

A

Yes. When accessing from outside of Struct, full path is required Exp. Struct.Enum.case

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

Can you use a function as a closure?

A

Yes, but it’s a code smell if you are not using it somewhere else.

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

Can you use index notation to access a specific tuple?

A

Yes

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

When you don’t label your closure function variables, are they still accessible?

A

Yes. $0, $1, $2 …

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

How do you decompose a tuple? into variables

A

let (FIRST, SECOND) = (1, 2)

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

I want to sort an array in place what method can I use?

A

array.sort(by:) { }

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

What’s the syntax to label closure parameters?

A

after curly braces wrap parameter names in parenthesis followed by the “in” keyword.

list.map { (item) in
}

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

What String method can you use to return a Bool if string ends with certain characters?

A

”“.hasSuffix(“ending”)

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

When is it appropriate to omit the return value when writing a closure

A

If the calling method’s closure parameter is the final or only parameter

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

How do you map over an array

A

array.map(transform:) { (item) in … }

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

Can you break up tuples using nested arrays into variables?

A

Yes.. Exp:

let (key, (value1, value2) = $0

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

What affect does adding underscores when storing a whole number have?

A

None. Swift ignores them

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

What method can you use to iterate over all items in a collection?

A

list.forEach(body:) { (item) in … }

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

How do you know that a method takes a closure?

A

Look in the “Developer Documentation.” The function definition will show a Function Type as a Type for the specified parameter.

Exp:
sort(by: (Element, Element) -> Bool)

(Element, Element) -> Bool ; Is the function type for the closure.

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

I want to sort an array without modifying the calling array, How do I do that?

A

let newArray = array.sorted(by:) { }

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

How you create a multi-line strings?

A

Use triple quotes to start and end multi-line strings

var text = “””
Text
“””

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

Can you add Emoji’s in a string?

A

Yes

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

How do you count the characters in a string?

A

use .count getter

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

What String method can you use to uppercase the characters?

A

.uppercased()

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

What String method can you use to get a Bool if string starts with certain characters?

A

”“.hasPrefix(“beginning”)

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

When storing long numbers, is it possible to break them down using comma’s?

A

No, but you can break them up using underscores instead.

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

Does Swift support shorthand operators?

A

Yes.

count += 1 ; count -= 1

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

What Int method can you use to get a Bool if integer is multiple of something?

A

120.isMultiple(of: 3)

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

What Bool method can you use to flip a boolean to the opposite value?

A

.toggle()

exp:
var isOpen = true

isOpen.toggle()

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

What is a “Half-open Range Operator?”

A

1..<10 - Range between 1 and 10 excluding the last number, so 1-9.

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

What does “Operator Overloading” mean?

A

Allows the same operators (+-*/, and so on) to do different things depending on the data type you use them with

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

What is a “Compound Assignment Operator?”

A

Syntactical sugar for var = var + 1

var += 1

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

What affect does using “Comparison operators” on two strings?

A

Compares if they are in alphabetical order

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

Do you need to explicitly add a break statement to each switch case?

A

No. Swift only matches one case, if you need you need to more on to the next use the keyword: “fallthrough”

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

Example of a condition statement?

A

if a > 1 { } else if a < 1 {} else {}

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

What does “Conditional Short-Circuiting” mean?

A

Conditional expressions are evaluated from left-right, the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression.

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

Does Swift yell at you if you don’t use the value returned from a for .. in loop? If so, how do you avoid this?

A

Yes. Use an underscore as the value name to ignore it.

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

When building a conditional with multiple expressions, should you use parenthesis?

A

Yes. Specially if expression includes an OR expression

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

What is the “Ternary Operator?”

A

let answer: String = true ? “True” : “False”

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

When creating a switch statement, do you need to have a default case?

A

Yes. all cases must be exhaustive!

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

What is a “Closed Range Operator?”

A

1…10 - Range between 1 and 10 including the last number, so 1-10

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

How do you write a while loop?

A

while count <= 20 {
count += 1
}

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

When you “break” out of a loop that is a nested loop, will it break out of all the loops?

A

No. You will need to add a label to the outer loop to break out of nested loops

outerLoop: for..in { for..in { break outerLoop } }

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

How do you know to use for .. in or while loops?

A

For … in loop = Finite sequences

While loop = Unknown amount of sequences

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

When should you use the repeat {} while loop?

A

When you want your condition to execute at least once before checking the condition.

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

Example of repeat {} while

A
let numbers = [1, 2, 3, 4, 5]
var random: [Int]

repeat {
random = numbers.shuffled()
} while random == numbers

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

What Array method can you use to create, and shuffle a new copy of an array?

A

[].shuffled()

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

What character can you use to ignore the value returned?

A

Use underscore (_)

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

Can you use range operators as switch cases?

A

Yes

case 1…10:
print(“numbers 1 to 10”)

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

What keyword can you use to exit loops immediately?

A

The “break” keyword

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

Can you give labels to loops (a.k.a “Label Statements”)?

A

Yes. with labelName followed by a colon

initialLoop: for-loop {}

73
Q

Do you use the “break” keyword to skip the current iteration?

A

No. You use the “continue” keyword.

74
Q

What is the difference between the “break” and “continue” keyword?

A

“break” exits out of the loop/s and “continue” skips to the next iteration.

75
Q

What condition can you use to create an infinite-loop?

A

while true {}

76
Q

What’s an example of a function?

A
func sayHello(name: String) -> String {
  print("Hello, \(name). Welcome!")
}

sayHello(name: “Ruben”)

77
Q

What is an “Expression?”

A

When a piece of code can be boiled to a single value.

e.g., true, false, “Hello, world”, 1337

78
Q

What is a “Statement?”

A

When we’re performing actions such as: creating variables, starting loops, checking conditions

79
Q

Are the following items an expression or statement?

3 > 1, true, false, “Boo”, 1010

A

Expression

80
Q

Statement or expression?

let ans: Int = 100

A

Statement

81
Q

Can you skip the “return” keyword when returning from a function if what you are returning is an expression?

A

Yes. but it has to be the only thing within the function. Function body cannot contain any statements

82
Q

Is the “Ternary Operator” an expression or statement?

A

Expression

83
Q

Is the “Ternary Operator” used in SwiftUI often?

A

Yes

84
Q

Can a function return multiple values?

A

Yes. You using a tuple.

```
Example:
func makeRequest() -> (error: String, statusCode: Int) {
error: “ERROR”, statusCode: 200
}
~~~

85
Q

Does Swift support an internal and external parameter label?

A

Yes, provide two labels. The first label is the external label, second is the internal label.

func sayHello(to name: String) -> {
  print("Hello, \(name).")
}

sayHello(to: “Bob”)

86
Q

Can you omit a function label?

A

Yes. Use the underscore (_) character in place the external parameter label

87
Q

Is this code valid? If valid, what is the output?

func sayHello(to name: String = "Person") {
  print("Hello \(name). How are you?")
}

sayHello()

A

Yes. Output is: “Hello Person, How are you?”

88
Q

What is a “Variadic function?”

A

A function that accepts any number of parameters

89
Q

Do variadic function parameters need to be of the same type?

A

Yes

90
Q

Can you have multiple variadic parameters?

A

No. Only a single variadic parameter is permitted.

91
Q

Is a variadic parameter converted to an array inside the function?

A

Yes

92
Q

Can you pass an array to a variadic parameter in place of comma separated items?

A

No. It must be a list of comma separated items.

93
Q

When creating a variadic parameter, what do you add following the input type?

A

Three dots (…)

func nums(list: Int…) {}

94
Q

Can you throw structure instances?

A

Yes. Only use when Enums is not enough.

95
Q

What keyword does a function definition need to declare that it could possibly return and error.

A

The “throws” keywords. Must be added before the return arrow symbol (->)

Exp:
func saveFile(name: String) throws -> Bool {
}
96
Q

Is it possible to pass a parameter by reference so that it can be modified in place?

A

Yes. Use the keyword “inout” after the colon following the parameter label.

97
Q

What kind of enum would you use if you want a case to have additional text associated with it?

A

Enum with associated values

98
Q

What keyword do you use to raise an error?

A

The “throw” keyword

99
Q

How do you explicitly show that you are passing a variable to a function as a reference instead of value?

A

Using an ampersand (&) before the variable name when calling a function.

100
Q

When catching a thrown error, how do you print out the value associated with the associated value case statement?

A

catch Enum.caseWithValue(let message) {
print(“My associated msg: (message)”)
}

101
Q

Do Enums and Structs must conform to input type “ThrowError” when used for throwing?

A

No. They must conform to an “Error” Input Type.

102
Q

What is an example of a running a throwing function?

A
do {
  try functionThatThrows()
} catch {
}
103
Q

When calling a function that expects an “inout” parameter do I have to do anything special?

A

Yes. use an ampersand (&) before the variable name.

var value = "My String"
func(inOutParam: &value)
104
Q

When passing parameters to functions are they immutable?

A

Yes. They cannot be modified. They are constants.

105
Q

When should you mark a function parameter as an “inout” parameter?

A

When you want your function to modify the parameter inside the function.

106
Q

What Array method can you use to remove an element from an array?

A

[].remove(at:) – Zero indexed

107
Q

What 3 keywords rely on properly calling a throwing function?

A

“do”, “try” and “catch”

108
Q

Inout parameters must be passed in using what symbol?

A

Ampersand (&) before the variable name.

109
Q

Inout parameters are marked using what keyword?

A

“inout”

110
Q

What mechanism can you use to return multiple parameters from a function?

A

Tuple; represented by open and closing parenthesis.

Exp: (One, Two, Three)

111
Q

What Array method can you use to add items to the end of an array?

A

[].append(“last_item”)

112
Q

Can you concatinate two arrays into one using the addition sign?

A

Yes.

[1,2,3] + [4,5,6] = [1,2,3,4,5,6]

113
Q

How do you create an empty array of type Characters?

A
var letters = [Character]()
or
var letters = Array|Character|()
or
var letters: [Character] = []
114
Q

What Array method can you use to count the number of elements?

A

[].count

115
Q

Can a Set have duplicates?

A

No

116
Q

Is the Array method [].remove(at:) zero indexed?

A

Yes

117
Q

What Array method can you use to remove all items?

A

[].removeAll()

118
Q

What Array method can you use to check if the array contains a certain element?

A

[].contains()

119
Q

How do you return a new sorted array?

A

[].sorted()

120
Q

What is thhe difference between an Array and a Set?

A

Sets are unordered and cannot contain duplicates, whereas arrays retain their order and can contain duplicates. Sets are highly optimized for fast retrieval than arrays.

121
Q

When you reverse an array, does it actually reverse the array?

A

No. It returns a ReversedCollection type that when used will iterate in reverse. This is an optimization feature.

122
Q

What method can you use to reverse an array?

A

[].reversed()

123
Q

When checking if an item is included in a Set, can you use the “contains” keyword as you with Arrays?

A

Yes. set.contains()

124
Q

When accessing an index that does not exist in an array, will it return an optional nil?

A

No. The application will crash.

125
Q

How do you create an empty dictionary where the key type is an Int and the value type is a String

A
var agesToName = [Int: String]()
or
var agesToName = Dictionary|Int, String|()
or
var agesToName: [Int: String] = [:]
126
Q

When retrieving a value from a dictionary, can you also provide a value in case that key does not exist in the dictionary?

A

Yes. dic[“myKey”, default: “NotFound”]

127
Q

Can the default value of a dictionary retrieval be any type?

A

No, it must be the value type of the dictionary.

128
Q

How do you count how many items are in a dictionary?

A

dic.count getter

129
Q

How do you remove all items from a dictionary?

A

dic.removeAll()

129
Q

When asking a dictionary for a value, do you get back an optional?

A

Yes, unless you explicitly set the default: parameter

Yes = dic["myKey"]
No = dic["myKey", default: "NotFound"]
131
Q

Are dictionaries more performant?

A

Yes, because the way they are stored and optimized allows for retrieval to be much faster than having to iterate over an array to find the value you want.

132
Q

When adding items to a Set, can you use the “append” keyword as you do with Arrays?

A

No. You use set.insert(). “append” no longer makes sense because there is no actual order.

133
Q

Can you sort a Set?

A

Yes. set.sorted()

134
Q

How do you declare an empty Set?

A
var mySet = Set|String|()
or
var mySet: Set|String| = []
or
var mySet: Set = ["one", "two", "three"]
135
Q

When redefining a variable of type enum, do you always have to use the full EnumNameType.caseStatement?

A

No. once a variable is of type enum, you can simply use dot syntax.

exp:
var test = MyEnumType.hello
test = .goodbye

136
Q

What is “Type Inference”?

A

When Swift infers the type of a variable or constant.

137
Q

What is “Type Annotation”?

A

Let’s us be explicit about what data types we want.

138
Q

Why is the following returning an error? What can you do to fix it it?

var num1  = 2
var num2 = 3.5

print(num1 + num2)

A

Swift is infering num1 as an Int and num2 as a Double. You cannot add two variables of different types.
There are multiple way to fix it: 1. Make num1 = 2.0, or cast num1 as a Double, or add type annotation to num1.

139
Q

How do you create an empty array of strings?

A

var myArray = String

140
Q

Should you always prefer Type annotation?

A

No. Prefer Type Inference and use Type annotation when necessary.

141
Q

What is a good exception when Type annotation is preferred or required?

A

When you are declaring a constant that you don’t know the value yet.

142
Q

Will this build?

let score: Int = “Zero”

A

No. Swift cannot convert the string “Zero” to an Int.

143
Q

What value does Swift store?

var percentage: Double = 99

A

99.0

144
Q

Why does Swift have type annotations?

A
  1. Swift can’t figure out what type should be used.
  2. You want Swift to use a different type from its default.
  3. You don’t want to assign a value just yet.
145
Q

How do you create a new Set out of an existing array?

A

var newSet = Set(myOldArray)

146
Q

What are the following called?

greater than, less than, ==, !=, greater than equal to, less than equal to

A

Comparison Operator Symbols

147
Q

How do you remove the first element in an array?

A

myArray.remove(at: 0)

148
Q

Can you use greater than and less than to compare two items of type ‘String’ ? If so, what does it do?

A

Yes. It will tell you if one is before the other in alphabetical order.

149
Q

The following code checks if a variable of type String is empty. Is this the most performant? If not, then what is?

if myString.count == 0 { }

A

No. In Swift, unlike other languages, when getting the count Swift check every element and counts up. Better method is to use “isEmpty”

if myString.isEmpty { }

150
Q

How are variables able to be compared using the Comparison operator?

A

Because the types conform to the “Comparable” Protocol.

151
Q

Why does the following return true?

enum Sizes: Comparable {
    case small
    case medium
    case large
}
let first = Sizes.small
let second = Sizes.large
print(first < second)
A

Because small comes before large in the enum case list.

152
Q

Why would you add parenthesis in a if condition expression?

A

When using multiple conditions and you want to be explicit to Swift and developers what order the expression should be evaluated.

153
Q

In Swift do Switches need to be exhaustive?

A

Yes. All cases need to be accounted for.

154
Q

When writing a switch and you can’t possibly write down all cases, you can use an “else” case statement to define all other cases. Is this true?

A

No. You use the “default:” case

exp:
switch thing {
  case one:
    print("Hi")
  default:
    print("All others")
}
155
Q

You have to be explicit in your case statements by adding a “break” statement at the end of each case or else it will continue to the next case down the list. Is this true?

A

No. It’s actually the other way around. After evaluation the correct switch statement Swift will stop evaluation the rest. If you want it to continue down the list. You must add the “fallthrough” statement at the end.

156
Q

Can you use switch on an enum?

A

Yes. You must remember to account for every enum case. You must be exhaustive.

switch myEnum {
case .optionOne:
case .optionTwo:
}

157
Q

What does it mean that switches must be exhaustive?

A

You must either have a case block for every possible value to check (e.g. all cases of an enum) or you must have a default case.

158
Q

Does Swift require that its switch statements are exhaustive?

A

Yes

159
Q

Why use a Switch statement instead of if/else if/else?

A
  1. Switches must be exhaustive, so you will avoid missing a specific case.
  2. Switch only checks the statement once, where as a condition might be written to check on every “else if” block.
  3. Switches allow for advanced pattern matching.
  4. When you have more than three conditions to check. Switch statements are often clearer to read than multiple if conditions.
160
Q

Refactor the following code:

let test: String
if true {
    test = "Must be true"
} else {
    test = "Wrong!"
}
A

Use the “Ternary Conditional Operator Expression”.

let test = true ? “Must be true” : “Wrong!”

161
Q

What is a good Mnemonic for the Ternary conditional operator expression?

A

WTF ; What to test, True expression, False expression

162
Q

How many pieces of input do Ternary conditional operators operate on?

A

Three. ; Thus the name Ternary

163
Q

How many pieces of input do the following operate on: +, -, ==

A

Two. ; Thus the name Binary

164
Q

Binary operators operate on how many pieces of input?

A

Two pieces of input

165
Q

Ternary operators operate on how many pieces of input?

A

Three pieces of input

166
Q

When using a loop, what do you call one cycle through the loop body?

A

A loop iteration

167
Q

Is this valid code?

names = [“john”, “bob”]
for name of names {
print(“Hello!”)
}

print(name)

A

No. The loop variable is only available within the loop body.

168
Q

In a for..in loop, what do you call the content between the braces?

A

The Loop Body

169
Q

When you have nested loop and are counting, what is a common convention for the loop variable name?

A

First loop: (i), second inner loop: (j), third inner loop: (k). Any further you probably should pick different loop variable names.

170
Q

How do you pronounce the range: 1…5

A

One through Five

171
Q

How do you pronounce the range: 1..<5

A

One up to Five

172
Q

What kind of range does the following create: x…y ?

A

Closed Range Operator: Starts at whatever the number x is, and counts up to and including whatever the number y is.

173
Q

What kind of range does the following create: x..

A

Half-Open Range Operator: Starts at whatever the number x is, and counts up to and excludes whatever the number y is.

174
Q

A range number always have two parts. The following will return an error. True or False?

names = ["john", "bob"]
names2 = names[1...]
A

False. If you do not specify a second number in the range, it means start at x and continue until the end of the array.

175
Q

When is the half-open range operator helpful when working with arrays?

A

When we count from 0 and often want to count up to but excluding the number of items in the array.

176
Q

When are while loops useful?

A

When you just don’t know how many times the loop needs to go around.

177
Q

How do you create a random number?

A

Int.random(in:) or Double.random(in:)

178
Q

When are for loops useful?

A

When you have a finite amount of data to go through, for example, ranges, arrays or even Sets.