Getting started
Warm-up problems on evaluating expressions by hand, reading Swift’s two kinds of error, and choosing types.
- Lessons
- Getting started
CS61A discussions are for working through problems together, out loud. Try each question before opening its solution, ideally with someone else; explaining why an answer is right is the skill being practiced. These go with Lesson 1.
Q1: Evaluate by hand
Evaluate each expression the way Swift does, one step at a time: operands first, then the operator; innermost calls first. Write each intermediate value.
max(3, min(8, 2 * 3))10 - 2 * 3 + 7 % 4min(max(-1, -5), abs(-4))
Show solution
2 * 3is 6;min(8, 6)is 6;max(3, 6)is 6.2 * 3is 6 and7 % 4is 3, since*and%come before-and+; then10 - 6 + 3is 7, evaluated left to right.max(-1, -5)is −1 andabs(-4)is 4;min(-1, 4)is −1.
Q2: What would Swift print?
What would Swift print?
print(9 / 2 * 2, 9 * 2 / 2)Show answer and explanation
Answer8 9
Left to right: 9 / 2 is 4 (integer division), times 2 is 8. In the second,
9 * 2 is 18, divided by 2 is 9. The order of operations changes the answer
when integer division throws a remainder away.
What would Swift print?
let a = 3
let b = a + 1
print(a * b, "a * b")Show answer and explanation
Answer12 a * b
The first argument is an expression, evaluated to 12. The second is a string literal, printed exactly as written.
What would Swift print?
print(max(1.5, 2), max(1.5, 2) / 2)Show answer and explanation
Answer2.0 1.0
The literal 2 becomes a Double to match 1.5, so max returns 2.0, and
2.0 / 2 is the Double 1.0. Doubles always print with a decimal point.
Q3: Compile time or run time?
For each program, decide whether it fails before running (compile time), while running (run time), or not at all.
let x: Int = 3.5let scores = [1, 2]thenprint(scores[2])print("total: " + 5)let n = 7thenprint(n / 2)
Show solution
- Compile time. 3.5 is not an integer, and Swift will not round it silently.
- Run time. The code is well formed; only when it runs does position 2 turn out not to exist.
- Compile time.
+cannot join aStringand anInt. Write"total: \(5)"or"total: " + String(5). - Not at all. It prints 3.
The general rule: anything the compiler can see from the types alone is caught before the program runs. Values that depend on what happens while running, like an array index, are checked at run time.
Q4: Pick a type
Which Swift type (Int, Double, String, or Bool) best represents each
of these?
- The number of students in a class.
- The average score on an exam.
- Whether a door is locked.
- A zip code, such as
02134. - A price in dollars and cents.
Show solution
Int: a count is a whole number.Double: an average can have a fraction.Bool: it has exactly two states.String: it is a label made of digits, not a quantity. Stored as anIntit would lose its leading zero, and nobody adds zip codes together.Intcounting cents is the safe answer. ADoublecannot represent 0.10 exactly, and money calculations that drift by fractions of a cent are a classic bug.
Q5: Seconds in a leap year
Write one expression, using only literals and arithmetic, for the number of seconds in a leap year. Then check it by computing seconds per day first.
Show solution
let secondsInLeapYear = 366 * 24 * 60 * 60
let secondsPerDay = 24 * 60 * 60
print(secondsInLeapYear) // 31622400
assert(secondsInLeapYear == 366 * secondsPerDay)Naming secondsPerDay is the first step toward the next lesson’s theme:
giving a computation a name makes it reusable and easier to check.