Collection Types Flashcards

1
Q

What are Swift’s three primary collection types?

A

Arrays, sets, and dictionaries.

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

What do collection types collect?

A

Values.

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

What is an array?

A

An ordered collection of values.

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

What is a set?

A

An unordered collection of unique values.

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

What is a dictionary?

A

Unordered collections of key-value associations.

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

If assigned to a variable, are collections mutable or immutable?

A

Mutable, it’s size and contents can be changed.

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

If assigned to a constant, are collections mutable or immutable?

A

Immutable, it’s size and contents cannot be changed.

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

An array stores values of the _____ type in an _______ list.

A

Same, ordered.

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

In an array, can the same value appear multiple times at different positions?

A

Yes.

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

How do you write the type of array?

A

Array where element is the type of value the array is allowed to store.

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

How do you create an empty array of a certain type using initializer syntax? (Give an example using someInts as the var)

A
var someInts = [Int]()
     someInts = [] (if there is already context)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Create an array with a default value (3 doubles of equal value)

A

var threeDoubles = Double

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

Create an array by adding two arrays together.

A

Add name of first array variable and second array variable together. EX: var sixDoubles = threeDoubles + anotherThreeDoubles

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

What is an array literal?

A

A shorthand way to write one or more values as an array collection. EX: var shoppingList: [String] = [“Eggs”, “Milk”]

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

You can access and modify an array by using what kind of syntax?

A

Subscript. EX: var firstItem = shoppingList[0]

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

Name two ways to add an item to the end of an array.

A

Use the method .append(_:)

nameOfArray += [“Item to add”]

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

The first item in an array has a value of what?

A

Zero.

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

Use subscript syntax to change the first item in the shoppingList array to “Six eggs”.

A

shoppingList[0] = “Six eggs”

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

How do you replace a range of values in an array?

A

shoppingList{4…6] = [“Bananas”, “Apples”]

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

Can you use subscript syntax to append a new item to the end of an array?

A

No.

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

How would you insert “Maple Syrup” to value zero in the shoppingList array?

A

shoppingList.insert(“Maple Syrup”, atIndex: 0)

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

How would you remove “Maple Syrup” from value zero in the shoppingList array?

A

let mapleSyrup = shoppingList.removeAtIndex(0)

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

How would you remove the last item in the shoppingList array (“apples”)?

A

let apples = shoppingList.removeLast()

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

How would you iterate over the entire set of values in the shoppingList array?

A
for item in shoppingList {
 print(item)
 }
 // Six eggs
 // Milk
 // Flour
 // Baking Powder
// Bananas
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

How would you enumerate each item of the shoppingList array?

A
for (index, value) in shoppingList.enumerate() {
print("Item \(index + 1): \(value)")
}
// Item 1: Six eggs
// Item 2: Milk
// Item 3: Flour
// Item 4: Baking Powder
// Item 5: Bananas
26
Q

In sets, do items appear once or more than once?

A

Once.

27
Q

What is a hash value?

A

An Int value that is the same for all objects that compare equally, such that if a = b, it follows that a.hashValue == b.hashValue.

28
Q

Are all of Swift’s basic types (String, Int, Double, Bool) hashable by default?

A

Yes.

29
Q

How is a type of a set written?

A

Set where Element is the type that the set is allowed to store.

30
Q

Create an empty set of a certain type (Character) using initializer syntax for the variable “letters”.

A

var letters = Set()
OR
letters = []

31
Q

Can you initialize a set with an array literal as a shorthand way to write one or more values as a set collection?

A

Yes.

32
Q

How would you create a set (“favoriteGenres”) consisting of Rock, Classical, and Hip Hop with an array literal?

A

var favoriteGenres: Set = [“Rock”, “Classical”, “Hip hop”]

33
Q

Can a set type be inferred from a literal array alone?

A

No. The type must be explicitly declared.

34
Q

How do you access and modify a set?

A

Through its methods and properties.

35
Q

How would you check to see if favoriteGenres includes Funk?

A

if favoriteGenres.contains(“Funk”)

36
Q

How do you iterate over the values in a set?

A

With a for-in loop.

for genre in favoriteGenres {
print("\(genre)")
}
// Classical
// Jazz
// Hip hop
37
Q

How would you iterate over the values of a set in a specific order?

A
for genre in favoriteGenres.sort() {
print("\(genre)")
}
// Classical
// Hip hop
// Jazz
38
Q

Name four fundamental set operations.

A

Intersect, exclusiveOr, union, subtract.

39
Q

What method creates a new set with only the values common to both sets?

A

.intersect(_:)

40
Q

What method creates a new set with values in either set, but not both?

A

.exclusiveOr(_:)

41
Q

What method creates a new set with all of the values in both sets?

A

.union(_:)

42
Q

What method creates a new set with values not in the specified set?

A

.subtract(_:)

43
Q

What is a superset?

A

A set that contains all the elements of another set and of itself.

44
Q

What is a subset?

A

A set that’s elements are also contained in another set.

45
Q

What are disjoint sets?

A

Two sets that do share no common elements.

46
Q

What method determines whether all of the values of a set are contained in the specified set?

A

.isSubsetOf(_:)

47
Q

What method determines whether a set contains all of the values in a specified set?

A

.isSupersetOf(_:)

48
Q

What methods determine whether a set is a subset or superset, but not equal to, a specified set?

A

.isStrictSubsetOf(_:)

.isStrictSupersetOf(_:)

49
Q

When do you use a dictionary?

A

When you need to look u values based on their identified, in much the same way that a real-world dictionary is used to look up the definition of a particular word.

50
Q

Create an empty Dictionary (namesOfIntegers) of a String type by using initializer syntax.

A

var namesOfIntegers = Int: String

51
Q

Can Swift infer set type information?

A

Yes, it can.

52
Q

Create a namesOfIntegers empty dictionary with an empty dictionary literal.

A

namesOfIntegers = [:]

53
Q

Create a dictionary to store the names of international airports with a key standing for the airport’s three-letter code and the value as the airport name.

A

var airports: [String: String] = [“YYZ”: “Toronto Pearson”, “DUB”: “Dublin”]

54
Q

What dictionary method sets or updates the value for a particular key?

A

.updateValue(_:forKey:)

55
Q

The .updateValue(_:forKey:) method returns what of the dictionary’s value type?

A

An optional value (e.g., String? or “optional String” with the optional string containing the old value for that key if one existed before.)

56
Q

You can remove a key-value pair from a dictionary by assigning what value for that key?

A

nil

57
Q

What method removes a key-value pair from a dictionary?

A

.removeValueForKey(_:)

58
Q

How can you iterate over the key-value pairs in a dictionary?

A

With a for-in loop.

for (airportCode, airportName) in airports {

print("\(airportCode): \(airportName)")
}
// YYZ: Toronto Pearson
// LHR: London Heathrow
59
Q

How can you retrieve an utterable collection of dictionary’s keys or values?

A

By accessing its keys and values properties.

for airportCode in airports.keys {
print(“Airport code: (airportCode)”)
}

// Airport code: YYZ
// Airport code: LHR

for airportName in airports.values {
print(“Airport name: (airportName)”)
}

// Airport name: Toronto Pearson
// Airport name: London Heathrow
60
Q

If you need to use a dictionary’s keys or values with an API that takes an array instance, what should you do?

A

Initialize a new array with the keys or values property.

let airportCodes = [String](airports.keys)
// airportCodes is ["YYZ", "LHR"]
let airportNames = [String](airports.values)
// airportNames is ["Toronto Pearson", "London Heathrow"]
61
Q

What method should you use to iterate over the keys or values of a dictionary in a specific order?

A

.sort()