Sample MECH&AE 20P questions
  1. Expressions, Types and Memory · Memory model · 1 mark

    Consider the following code.

    steps = 4000
    goal = steps
    steps = steps * 2

    Which statement is true after the code runs?

    1. goal is 8000, because goal and steps refer to the same object.

    2. goal is 4000, and steps refers to a different object than goal.

    3. goal is 4000, and steps still refers to the same object as goal, whose value was changed to 8000.

    4. Line 3 raises an error because steps is already in use by goal.

    Show solution

    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.

  2. 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 ____________________
    Show solution
        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).

  3. 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)
    Show solution
    121212 36
    312 31
    4.0 1
    ERROR

    int('007') is 7, whose string form has length 1; '12' + 3 is a TypeError.

  4. 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
        """
    Show solution
        total = 0
        for i in range(len(square)):
            for j in range(i + 1):
                total = total + square[i][j]
        return total

    Looping over every j and testing if j <= i is also correct. Common mistake: range(i), which leaves out the diagonal.

  5. 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.

    1. Would you use the course insertion_sort or selection_sort to re-sort the list? Explain in one or two sentences. [2]

    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]

    Show solution

    (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 1000×999/2≈500,0001000 \times 999 / 2 \approx 500{,}000 comparisons no matter how sorted the list is.
    (b) 999 (when the new timestamp is smaller than all the others).

About MECH&AE 20P practice exams

MECH&AE 20P practice exams

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.

Looking for MECH&AE 20P past exams?

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.

Topics covered

  • Expressions, Types and Memory · 100 questions
  • Booleans and Conditionals · 100 questions
  • For and While Loops · 100 questions
  • Functions and the Design Recipe · 100 questions
  • Strings · 100 questions
  • Lists, Mutability and Aliasing · 100 questions
  • Nested Lists and Loops · 100 questions
  • Reading and Writing Files · 100 questions
  • Classes and Objects · 100 questions
  • Searching, Sorting and Complexity · 100 questions

More UCLA courses

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.