All topics
Strings and Collections
`String` and `Character`, Unicode and string indices, arrays, dictionaries and sets — how Swift stores, reads and copies them.
Sort:
11 questions
JuniorTheoryCommonWhat does the program print?
JuniorTheoryCommon
What does the program print?
3 4
Open full question →Common mistakes
- ✗Expecting
4 4: thinkingvar list = startgives a second name to the same array. An array is a value type, solistis a separate copy. - ✗Expecting a compile error: thinking a copy of a
letarray stays constant. The copy follows its own declaration, andlistis avar.
Follow-up questions
- →When does Swift actually copy the elements of
start? - →How can a function add an element to the caller's array?
JuniorTheoryCommonWhat happens when this program is compiled and run?
JuniorTheoryCommon
What happens when this program is compiled and run?
The program crashes
Open full question →Common mistakes
- ✗Expecting
30: takingcountfor the index of the last element. Indices start at zero, so the last one iscount - 1, and indexcountcrashes the program. - ✗Expecting
nil: reading by an index gives a plainInt, not an optional likefirstandlast. An index that does not exist stops the program withIndex out of range.
Follow-up questions
- →How do you read the last element of
priceswithout going past the bounds? - →What does
prices.last ?? 0print, and why does it never crash?
JuniorTheoryCommonWhat does the program print?
JuniorTheoryCommon
What does the program print?
2 4
Open full question →Common mistakes
- ✗Expecting
1 4: readingdefault: 0as "set the count to 0". The default is used only while the key is missing; after that+= 1adds to the stored count. - ✗Expecting
2 6: takingcountfor the number of words added. A dictionary counts pairs, and a repeated word updates its pair instead of adding one.
Follow-up questions
- →What does
counts["is", default: 0]return here, and doescounts.countchange after it? - →Why does
counts[word] += 1withoutdefault:not compile?
JuniorTheoryCommonWhat does the program print?
JuniorTheoryCommon
What does the program print?
3 0 -1
Open full question →Common mistakes
- ✗Expecting a crash, as with an array index past the end. A dictionary lookup of a missing key gives
nil, and??replaces it with-1. - ✗Expecting
-1for"pears": treating a stored0as an empty value.??replaces onlynil, and the key"pears"exists.
Follow-up questions
- →How can you check whether the key
"pears"is in the dictionary at all? - →What does
stock["plums", default: -1]give, and does it add the key?
JuniorTheoryCommonWhat does the program print?
JuniorTheoryCommon
What does the program print?
2 3
Open full question →Common mistakes
- ✗Expecting
3 3: numbering the elements from1.enumerated()starts at0, so the last number in an array of three elements is2. - ✗Expecting
4 3: thinking the tuple holds the element first and the number second. The number comes first, soigets0,1and2.
Follow-up questions
- →What does the program print if the loop is
for (_, x) in scores.enumerated()withposition = x? - →How would you print each element together with its place counted from 1?
JuniorTheoryCommonWhat happens when this program is compiled and run?
JuniorTheoryCommon
What happens when this program is compiled and run?
It does not compile
Open full question →Common mistakes
- ✗Expecting
[1, 2, 3, 4]: thinkingletfixes only the name. In Swift an array declared withletis a constant as a whole, soappenddoes not compile. - ✗Expecting a crash on the
appendline: Swift checks changes to constants while compiling, before a single line runs.
Follow-up questions
- →What does the program print if
numbersis declared withvar? - →Which of
numbers + [4]andnumbers.append(4)works with aletarray, and why?
JuniorTheoryCommonWhy doesn't contains on a Set check the elements one by one?
JuniorTheoryCommon
Why doesn't contains on a Set check the elements one by one?
The set computes the value's place from the value itself and looks only there
Common mistakes
- ✗Thinking
Hashablemakes comparisons faster. It gives a number, the set computes the value's place from it, andcontainslooks only there. - ✗Expecting a
Setto keep its elements sorted and search by halving. A set keeps no order: it finds a value at the place computed from that value.
Follow-up questions
- →Why must the keys of a dictionary be
Hashabletoo? - →How does
containssearch an array, and why is that slower for a long array?
JuniorTheoryCommonWhat does the program print?
JuniorTheoryCommon
What does the program print?
5 4
Open full question →Common mistakes
- ✗Expecting
5 5: thinkinginsertalways adds, likeappend. A set skips a value it already holds, so inserting9again changes nothing. - ✗Expecting
5 2: reading "no repeats" as "appears once".Set(pack)keeps every distinct value once, so4and7stay too.
Follow-up questions
- →What does
print(album.sorted())print here? - →Why can
print(album)list the elements in a different order on each run?
JuniorTheoryCommonWhat does the program print?
JuniorTheoryCommon
What does the program print?
2 red-blue
Open full question →Common mistakes
- ✗Expecting
3 red--blue: thinkingsplitkeeps an empty piece between two separators. Swift'ssplitdrops empty pieces by default, sored,,bluegives two parts. - ✗Expecting
2 red-blue-: thinkingjoinedadds the separator after every part. It puts the separator only between parts, so nothing follows the last one.
Follow-up questions
- →How do you make
splitkeep the empty piece between the two commas? - →What does the program print if
joinedgets the empty separator""?
JuniorTheoryCommonWhat does the program print?
JuniorTheoryCommon
What does the program print?
6 8
Open full question →Common mistakes
- ✗Expecting
6 6: treatingéas one byte like a Latin letter. In UTF-8 it takes two bytes, so the twoéadd two extra bytes toutf8.count. - ✗Expecting
8 8: thinkingcountcounts bytes, as string length does in some languages. In Swiftcountcounts characters, and bytes are counted byutf8.count.
Follow-up questions
- →What do
countandutf8.countgive for the emoji👍🏽, and why? - →Why does Swift have to walk the whole string to find its
count?
JuniorTheoryCommonWhat happens when this program is compiled and run?
JuniorTheoryCommon
What happens when this program is compiled and run?
It does not compile
Open full question →Common mistakes
- ✗Expecting
S, ass[0]gives in many languages. Characters in Swift take different numbers of bytes, so a string can't be indexed with anInt, andword[0]does not compile. - ✗Expecting a crash at run time, as with an index past the end. Here the compiler already rejects indexing with an
Int, so the program never starts.
Follow-up questions
- →How do you get the first character of
wordwithoutword[0]? - →How do you get the third character of a string through
String.Index?