Expressions, Types and Memory · Memory model · 1 mark
Consider the following code.
steps = 4000
goal = steps
steps = steps * 2Which statement is true after the code runs?
goal is 8000, because goal and steps refer to the same object.
goal is 4000, and steps refers to a different object than goal.
goal is 4000, and steps still refers to the same object as goal, whose value was changed to 8000.
Line 3 raises an error because steps is already in use by goal.
Answer: B
(B). steps * 2 creates a new int object (8000) and only steps is updated to refer to it. int objects are immutable, so the object goal refers to still holds 4000.
For and While Loops · Complete the code · 3 marks
Fill in the blanks to complete the function. Do not change any other code.
def get_rating() -> int:
"""Prompt the user until they enter a whole number from 1 to 5
(digits only), and return that number.
"""
answer = input('Rating (1-5): ')
while ______________________________________________________:
print('Invalid rating.')
answer = ____________________________________
return ____________________ while not answer.isdigit() or int(answer) < 1 or int(answer) > 5:
print('Invalid rating.')
answer = input('Rating (1-5): ')
return int(answer)The isdigit test must come first: for input such as 'abc', short-circuiting stops int(answer) from being evaluated (it would raise an error).
Strings · Predict the output · 2 marks
Write the output of the code fragment. If the code raises an error, write ERROR and stop.
a = '12'
b = 3
print(a * b, int(a) * b)
print(str(b) + a, int(str(b) + a) // 10)
print(float(a) / b, len(str(int('007'))))
print(a + b)121212 36
312 31
4.0 1
ERRORint('007') is 7, whose string form has length 1; '12' + 3 is a TypeError.
Nested Lists and Loops · Write code · 2 marks
Complete the function body according to its docstring.
def lower_total(square: list[list[int]]) -> int:
"""Return the total of the items of square that are on or below
the main diagonal, that is, every square[i][j] with j <= i.
Precondition: square has the same number of rows and columns.
>>> lower_total([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
34
""" total = 0
for i in range(len(square)):
for j in range(i + 1):
total = total + square[i][j]
return totalLooping over every j and testing if j <= i is also correct. Common mistake: range(i), which leaves out the diagonal.
Searching, Sorting and Complexity · Short answer · 3 marks
A weather station keeps a list of 1000 timestamps that is always sorted. Each time a new timestamp arrives it is appended to the end, so the list is sorted except possibly for its last item, and then the whole list is sorted again.
Would you use the course insertion_sort or selection_sort to re-sort the list? Explain in one or two sentences. [2]
In this situation, what is the largest number of times the body of the while loop in insert can run during one call to insertion_sort? [1]
(a) Insertion sort. Each of the first 999 calls to insert makes at most one check because those items are already in order, and the last call shifts at most 999 items, so the work is linear. Selection sort always does about comparisons no matter how sorted the list is.
(b) 999 (when the new timestamp is smaller than all the others).
Introduction to Computer Programming with Python · University of California, Los Angeles (UCLA, UC Los Angeles) · also written MECH&AE20P
Make a free, printable MECH&AE 20P practice exam: choose the topics, the length and the difficulty above, and get an exam-style PDF with a full answer key. Each paper draws on 1,000 original questions and skips the ones you have already seen.
Start with the past exams and tests your instructor or the UCLA library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh MECH&AE 20P practice exams here on the same topics, with full answer keys, as many as you need. The questions here are original, not copies of past exams.
questionbank.dev is independent and not affiliated with University of California, Los Angeles. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.