CMPT 120 · Flashcards
CMPT 120 Final Review: Python Basics, Control Flow, Recursion, Files and Algorithms
32 termsSimon Fraser Universityby @studyboardUpdated October 2026
A study set written by the Studyboard team for the topics in this course's published outline. It isn't official course material from Simon Fraser University and doesn't contain real exam questions.
Terms in this set (32)
- In Python 3, what are 7 / 2, 7 // 2 and 7 % 2?7 / 2 = 3.5 (true division always gives a float), 7 // 2 = 3 (floor division) and 7 % 2 = 1 (remainder).
- What is −7 // 2 in Python, and why?−4. Floor division rounds down toward negative infinity, not toward zero.
- Evaluate 2 + 3 * 4 ** 2.50. Exponentiation first (4 ** 2 = 16), then multiplication (3 * 16 = 48), then addition (2 + 48 = 50).
- Mutable vs immutable types in PythonMutable objects can be changed in place: lists, dictionaries, sets. Immutable objects cannot: int, float, str, tuple. 'Changing' a string really creates a new string.
- What is the difference between = and == ?= assigns a value to a variable. == compares two values and gives True or False.
- What happens with '5' + 2, and how do you fix it?It raises a TypeError because a str and an int cannot be added. Convert first: int('5') + 2 gives 7, or '5' + str(2) gives '52'.
- For s = 'python', what are s[1:4], s[-1] and s[::-1]?s[1:4] = 'yth' (index 1 up to but not including 4), s[-1] = 'n' (last character), s[::-1] = 'nohtyp' (the reversed string).
- What values does range(2, 10, 3) produce?2, 5, 8. It starts at 2, steps by 3, and stops before reaching 10.
- When is a while loop a better choice than a for loop?When you don't know ahead of time how many iterations you need, such as repeating until the user enters valid input. A for loop suits iterating over a known sequence or range.
- break vs continuebreak exits the nearest enclosing loop immediately. continue skips the rest of the current iteration and goes on to the next one.
- What is short-circuit evaluation?In A and B, B is not evaluated if A is False; in A or B, B is not evaluated if A is True. This lets you write guards like: x != 0 and 10 / x > 1.
- return vs print in a functionreturn sends a value back to the caller so it can be stored or used. print only displays text. A function with no return statement returns None.
- Local vs global scopeA variable assigned inside a function is local to that function and disappears when it returns. Global variables are defined at module level; assigning to one inside a function requires the global keyword.
- a = [1, 2]; b = a; b.append(3). What is a now, and why?[1, 2, 3]. b = a makes b another name (alias) for the same list. To copy instead, use b = a[:] or b = list(a).
- list.append vs list.extendappend adds its argument as a single element: [1, 2].append([3, 4]) gives [1, 2, [3, 4]]. extend adds each element of an iterable: it gives [1, 2, 3, 4].
- Key facts about Python dictionariesThey map keys to values. Keys must be immutable (hashable), such as str, int or tuple. d[k] raises KeyError if k is missing, while d.get(k, default) returns the default instead.
- What two parts does every correct recursive function need?A base case that returns without recursing, and a recursive case that calls the function on a smaller problem moving toward the base case. Without a reachable base case you get a RecursionError.
- Trace fact(4) where fact(n) returns 1 if n == 0, else n * fact(n − 1).fact(4) = 4 * fact(3) = 4 * 3 * fact(2) = 4 * 3 * 2 * fact(1) = 4 * 3 * 2 * 1 * fact(0) = 24.
- Linear search vs binary searchLinear search checks items one by one: O(n), works on any list. Binary search repeatedly halves a SORTED list: O(log n).
- About how many comparisons does binary search need for a sorted list of 1,000,000 items?About 20 in the worst case, since 2^20 ≈ 1,000,000. Linear search could need up to 1,000,000.
- What is the Big-O running time of two nested loops that each run n times?O(n^2), because the inner body runs n × n times. Doubling n roughly quadruples the running time.
- Running time of selection sort and insertion sortBoth are O(n^2) in the worst case. Insertion sort is O(n) on an already sorted list, while selection sort is always O(n^2).
- Why use 'with open(name) as f:' to read a file?It automatically closes the file when the block ends, even if an error occurs. Each line read includes its trailing newline, so use line.strip() or rstrip() before processing.
- File modes 'r', 'w' and 'a''r' reads (the file must exist). 'w' writes, creating the file or erasing existing contents. 'a' appends to the end without erasing.
- Syntax error vs runtime error vs logic errorSyntax error: the code breaks Python's grammar and won't run. Runtime error: it crashes while running (e.g., ZeroDivisionError). Logic error: it runs but produces the wrong answer.
- What does a try / except block do?Code in try runs normally; if a matching exception is raised, control jumps to the except block instead of crashing. Catch specific exceptions (e.g., ValueError) rather than everything.
- What is pseudocode and why write it?An informal, language-independent description of an algorithm's steps. It lets you plan the logic before worrying about exact syntax.
- Convert decimal 13 to binary.1101, since 13 = 8 + 4 + 0 + 1. A bit is one binary digit; a byte is 8 bits, giving 256 possible values.
- What do the shell commands pwd, ls, cd and mkdir do?pwd prints the current directory, ls lists its contents, cd changes directory (cd .. goes up one level) and mkdir creates a new directory.
- What is an off-by-one error?A loop or index that goes one step too far or stops one step short, e.g., using range(1, n) when you meant 1 through n inclusive, which needs range(1, n + 1).
- Why is 0.1 + 0.2 == 0.3 False in Python?Floats are stored in binary, which cannot represent 0.1 exactly, so the sum is 0.30000000000000004. Compare floats with a tolerance, e.g., abs(a − b) < 1e-9.
- What does [x * x for x in range(4)] produce?[0, 1, 4, 9]. A list comprehension builds a new list by applying the expression to each item.
Something wrong with this flashcard set? Open it in Studyboard and choose Report.