Collections and Control Flow (Arrays, Dictionaries) (if, Switch ) Flashcards

1
Q

What are the 3 collection types in swift?

A

Array, dictionary and sets

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

When you concatenate two things, what must the have in common?

A

They must be of the same type. So String and string or [Int] and [Int]

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

Given an array:
let a = [2.0, 1.1, 2.3, 5.4]

What is a’s type?

A

[Double]

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

let a = [1,2,3]
a.insert(4, at: 3)

The code raises an error. Why?

A

The array is immutable and cannot be changed

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

let a = [1, 2, 3]
a[3]

The code above crashes. Why?

A

The index value provided is out of bounds

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

To get the 4th item out of an array, which index number do we use?

A

3

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

TF: An array preserves the order of its elements, i.e., it is an ordered collection.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
Remove the 5th item in this array.
var a = [10, 1, 12, 22, 96, 14]
A

a.remove(at: 4)

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

What exactly is a dictionary?

A

Set of key, value pairs

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

What happens when you try to get a value that does not exist in an array vs dictionary?

A

Array → it crashes

Dictionary → it will return nil because dictionaries are always of an optional type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
Given the following dictionary:
let dict = [1: "someValue", 2: "anotherValue"]

What is the dictionary’s type?

A

[Int : String]

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

When we try to access a value using a key that doesn’t exist in a dictionary, what result do we get?

A

A nil value

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

TF: The following code snippets are equivalent
dict.updateValue(“yetAnotherValue”, forKey: 3)
And
dict[3] = “yetAnotherValue”

A

True

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

TF: A dictionary is an ordered collection and preserves the order of key-value pairs added to it

A

False

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

What is an if statement?

A

if statement is a conditional statement that executes some code depending on the conditions stated.

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

If you have a bunch of possibilities in an if else statement, when will the condition be met?

A

As soon as a condition is satisfied from top to bottom, the code will be executed.

17
Q

Give all the possibilities when using && or ||.

A
True && True → True
True && False → False
False && False → False 
True || True → True
True || False → True 
False || False → False
18
Q

How does a switch statement work?

A
Switch (value to consider) {
	case value1: response to value1
	case value1: response to value2
	default : do this
}
19
Q
What's missing from this switch statement? 
let months = 1...12
for month in months {
    switch month {
        case 1:
            print("January")
    }
}
A

The above code will not compile because switch statements need to be exhaustive with case statements for all expected values or a default statement.

20
Q
var isAvailable = true
var isCheap = true

var status: String

if !isCheap && !isAvailable {
    status = "super rare"
} else if isCheap && isAvailable {
    status = "not rare"
} else if !isCheap && isAvailable {
    status = "somewhat rare"
} else {
    status = "common"
}

What does status evaluate to?

A

“Not rare”

21
Q

TF: Both the AND and OR operators use short circuit evaluation

A

True

22
Q

What does the following statement evaluate to?

!true || !false) && (true && !false

A

True