All topics
Operators and Control Flow
Arithmetic and integer division, overflow, comparisons and logic, ranges, `if`, `switch` and loops — what a program computes and which line runs next.
Sort:
9 questions
JuniorTheoryCommonWhat does the program print?
JuniorTheoryCommon
What does the program print?
out in
Open full question →Common mistakes
- ✗Expecting
in in: reading..<as including its upper bound.1..<5holds the numbers 1 to 4, socontains(5)isfalseandaisout. - ✗Expecting
out out, as in languages where a range never includes its end: in Swift the closed range1...5includes 5.
Follow-up questions
- →How many numbers does
0..<5hold, and which ones? - →When is
..<more convenient than...?
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
new mailbecause a non-zero number counts as true, as in C or JavaScript. In Swift a condition must be aBool; writeunread != 0. - ✗Expecting the program to stop only when it reaches the
ifline. The compiler checks the condition's type before the program starts, so nothing runs.
Follow-up questions
- →How would you rewrite the condition so the program prints
new mail? - →Which kinds of expressions can stand as the condition of an
ifin Swift?
JuniorTheoryCommonWhat does the program print?
JuniorTheoryCommon
What does the program print?
3 -1
Open full question →Common mistakes
- ✗Expecting
3 2, as in math: in Swift the remainder takes the sign of the dividend.-7 / 3is-2, and-2 * 3 + (-1)gives-7, so-7 % 3is-1. - ✗Expecting
3.5: dividing oneIntby another drops the fraction, so7 / 2is3. A fraction needsDoublenumbers, such as7.0 / 2.
Follow-up questions
- →What does
print(-7 / 2)print, and why not-4? - →How do you get
3.5from twoIntconstants holding7and2?
JuniorTheoryCommonWhat does the program print?
JuniorTheoryCommon
What does the program print?
2
Open full question →Common mistakes
- ✗Expecting
6: thinkingbreakalways ends only the innermost loop. With a label,break outerends the outer loop too, so there is no second outer pass. - ✗Expecting
3: thinkingbreaklets the current pass finish. It acts at once, socount += 1below it does not run whenjis3.
Follow-up questions
- →What does the program print if
break outeris replaced with a plainbreak? - →What would
continue outerdo in the same place?
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
-128: only&+wraps around like that. The plain+checks the result and crashes the program when the result does not fit the type. - ✗Expecting
127: Swift never clamps a result to the type's limit. A sum that does not fit inInt8stops the program instead.
Follow-up questions
- →What does
print(level &+ 1)print instead? - →Which type would you choose for
levelso that adding 1 here never crashes?
JuniorTheoryCommonWhat does the program print?
JuniorTheoryCommon
What does the program print?
1
Open full question →Common mistakes
- ✗Expecting
0: treatingrepeat-whilelikewhile.repeatruns the body first and checks the condition after it, so the body always runs at least once. - ✗Expecting
6: reading the loop as "repeat untilcount > 5". It repeats while the condition is true, and here it is false after the first pass.
Follow-up questions
- →What does the program print if the loop is rewritten as
while count > 5 { count += 1 }? - →When is
repeat-whilea better fit thanwhile?
JuniorTheoryCommonWhat does the program print?
JuniorTheoryCommon
What does the program print?
1 3 5
Open full question →Common mistakes
- ✗Expecting
1 3 5 7: treatingto:likethrough:. Withto:the end value is never part of the sequence, so the loop stops at5. - ✗Expecting the sequence to start at
3: assuming a step is taken before the first value. A stride starts atfromitself.
Follow-up questions
- →What does the loop print with
through: 7instead ofto: 7? - →How would you count down from 5 to 1 with
stride?
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
lowbecause the current value5has a branch. The compiler checks every possibleInt, so aswitchon a number needsdefault. - ✗Expecting a crash only when no branch fits. A missing branch is found by the compiler before the program runs, not while it runs.
Follow-up questions
- →What does the program print once you add
default: print("other")at the end? - →Why does a
switchon aStringneeddefaultas well?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
4
Open full question →Common mistakes
- ✗Expecting
4 3 2, as in C, where execution falls into the branches below. A Swift branch ends on its own; onlyfallthroughmoves into the next one. - ✗Expecting
defaultto run after the matching branch as well. It runs only when nocasefits the value.
Follow-up questions
- →What does the program print if you add
fallthroughafterprint(4)? - →How would you make one branch handle both
"B"and"C"?