Sample COMP 202 questions
  1. Expressions, Types and Memory · Predict the output · 2 marks

    Write the output of the following code fragment. If the code raises an error, write ERROR and stop.

    total = 10
    total += 5
    total *= 2
    print(total, end=' ')
    total //= 4
    print(total, end=' ')
    total -= total % 3
    print(total)
    Show solution
    30 7 6

    x op= y means x = x op y: 15, then 30, then 30 // 4 is 7, then 7 - 7 % 3 is 6.

  2. Booleans and Conditionals · Complete the code · 3 marks

    Consider the following function.

    def mystery(n: int) -> str:
        if n % 2 == 0 and n > 10:
            return 'A'
        elif n % 2 == 0:
            return 'B'
        elif n > 10 or n < 0:
            return 'C'
        elif n > 20:
            return 'D'
        else:
            return 'E'

    For each return value, give an argument for which mystery returns that value. If no argument does, write IMPOSSIBLE.

    'A''B''C''D''E'
                   
    Show solution

    Answer: 12; 4; 11; IMPOSSIBLE; 3

    'A': any even number above 10. 'B': any even number up to 10 (including negative ones). 'C': any odd number above 10 or below 0. 'D': IMPOSSIBLE, since any n > 20 is odd or even and is caught by an earlier branch. 'E': 1, 3, 5, 7 or 9.

  3. Lists, Mutability and Aliasing · Memory model · 3 marks

    Consider the code below.

    def reset(data: list[int]) -> None:
        data = []
    
    def clear(data: list[int]) -> None:
        while len(data) > 0:
            data.pop()
    
    x = [1, 2]
    y = x
    reset(x)
    print(x, y)
    clear(y)
    print(x, y, x is y)
    1. Write the output of the code. [2]

    2. Using the memory model, explain in one or two sentences why the call reset(x) does not empty the list that x refers to. [1]

    Show solution
    1. [1, 2] [1, 2]
      [] [] True
    2. data = [] only makes the local variable data in the reset frame refer to a new empty list. x in the main frame still refers to the original list, which is never mutated.

  4. Reading and Writing Files · Write code · 2 marks

    Complete the function body according to its docstring.

    def write_countdown(filename: str, n: int) -> None:
        """Write the integers from n down to 1 to the file called
        filename, one per line, followed by a final line Liftoff!
        Any existing contents of the file are replaced.
    
        Precondition: n >= 1
        """

    For example, after write_countdown('launch.txt', 3) the file launch.txt contains:

    3
    2
    1
    Liftoff!
    Show solution
        f = open(filename, 'w')
        for i in range(n, 0, -1):
            f.write(str(i) + '\n')
        f.write('Liftoff!\n')
        f.close()

    Common mistakes: f.write(i) (write needs a str) and forgetting the '\n'.

  5. Classes and Objects · Multiple choice · 1 mark

    Inside class Parcel, which header for __eq__ follows the course conventions and lets p1 == p2 compare two Parcel objects?

    1. def __eq__(other: 'Parcel') -> bool:

    2. def __eq__(self, other: 'Parcel') -> 'Parcel':

    3. def __eq__(self, other: 'Parcel') -> bool:

    4. def __eq__(self, p1: 'Parcel', p2: 'Parcel') -> bool:

    Show solution

    Answer: (C)

    (C). p1 == p2 calls p1.__eq__(p2): self is p1, other is p2, and the result is a bool.

About COMP 202 practice exams

COMP 202 practice exams

Foundations of Programming · McGill University (McGill, McGill U) · also written COMP202

Make a free, printable COMP 202 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,100 original questions and skips the ones you have already seen.

Looking for COMP 202 past exams?

Start with the past exams and tests your instructor or the McGill library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh COMP 202 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
  • Functions and the Design Recipe · 100 questions
  • Booleans and Conditionals · 100 questions
  • Strings · 100 questions
  • For and While Loops · 100 questions
  • Lists, Mutability and Aliasing · 100 questions
  • Nested Lists and Loops · 100 questions
  • Reading and Writing Files · 100 questions
  • Tuples and Dictionaries · 100 questions
  • Testing and Debugging · 100 questions
  • Classes and Objects · 100 questions

More McGill courses

questionbank.dev is independent and not affiliated with McGill University. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.