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
How would you enumerate each item of the shoppingList array?
``` 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
In sets, do items appear once or more than once?
Once.
27
What is a hash value?
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
Are all of Swift’s basic types (String, Int, Double, Bool) hashable by default?
Yes.
29
How is a type of a set written?
Set where Element is the type that the set is allowed to store.
30
Create an empty set of a certain type (Character) using initializer syntax for the variable “letters”.
var letters = Set() OR letters = []
31
Can you initialize a set with an array literal as a shorthand way to write one or more values as a set collection?
Yes.
32
How would you create a set (“favoriteGenres”) consisting of Rock, Classical, and Hip Hop with an array literal?
var favoriteGenres: Set = [“Rock”, “Classical”, “Hip hop”]
33
Can a set type be inferred from a literal array alone?
No. The type must be explicitly declared.
34
How do you access and modify a set?
Through its methods and properties.
35
How would you check to see if favoriteGenres includes Funk?
if favoriteGenres.contains(“Funk”)
36
How do you iterate over the values in a set?
With a for-in loop. ``` for genre in favoriteGenres { print("\(genre)") } // Classical // Jazz // Hip hop ```
37
How would you iterate over the values of a set in a specific order?
``` for genre in favoriteGenres.sort() { print("\(genre)") } // Classical // Hip hop // Jazz ```
38
Name four fundamental set operations.
Intersect, exclusiveOr, union, subtract.
39
What method creates a new set with only the values common to both sets?
.intersect(_:)
40
What method creates a new set with values in either set, but not both?
.exclusiveOr(_:)
41
What method creates a new set with all of the values in both sets?
.union(_:)
42
What method creates a new set with values not in the specified set?
.subtract(_:)
43
What is a superset?
A set that contains all the elements of another set and of itself.
44
What is a subset?
A set that’s elements are also contained in another set.
45
What are disjoint sets?
Two sets that do share no common elements.
46
What method determines whether all of the values of a set are contained in the specified set?
.isSubsetOf(_:)
47
What method determines whether a set contains all of the values in a specified set?
.isSupersetOf(_:)
48
What methods determine whether a set is a subset or superset, but not equal to, a specified set?
.isStrictSubsetOf(_:) | .isStrictSupersetOf(_:)
49
When do you use a dictionary?
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
Create an empty Dictionary (namesOfIntegers) of a String type by using initializer syntax.
var namesOfIntegers = [Int: String]()
51
Can Swift infer set type information?
Yes, it can.
52
Create a namesOfIntegers empty dictionary with an empty dictionary literal.
namesOfIntegers = [:]
53
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.
var airports: [String: String] = [“YYZ”: “Toronto Pearson”, “DUB”: “Dublin”]
54
What dictionary method sets or updates the value for a particular key?
.updateValue(_:forKey:)
55
The .updateValue(_:forKey:) method returns what of the dictionary’s value type?
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
You can remove a key-value pair from a dictionary by assigning what value for that key?
nil
57
What method removes a key-value pair from a dictionary?
.removeValueForKey(_:)
58
How can you iterate over the key-value pairs in a dictionary?
With a for-in loop. for (airportCode, airportName) in airports { ``` print("\(airportCode): \(airportName)") } // YYZ: Toronto Pearson // LHR: London Heathrow ```
59
How can you retrieve an utterable collection of dictionary’s keys or values?
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
If you need to use a dictionary’s keys or values with an API that takes an array instance, what should you do?
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
What method should you use to iterate over the keys or values of a dictionary in a specific order?
.sort()