Sample ESC180H1 questions
  1. Expressions, Types and Memory · Memory model · 2 marks

    Consider the following code.

    a = 'red'
    b = a
    a = a + 'dish'
    c = b

    Which statements are true after the code runs? Circle ALL that apply.

    1. b and c refer to the same object.

    2. a and b refer to the same object.

    3. The object that a referred to on line 1 now has the value 'reddish'.

    4. Exactly two different str objects are referred to by a, b and c.

    5. id(a) == id(c) is True.

    6. c == 'red' is True.

    Show solution

    Answer: A, D, F

    The first, fourth and sixth statements. Line 3 creates a new string object for a; b (and then c) still refer to the original 'red' object, which cannot be changed.

  2. Booleans and Conditionals · Multiple choice · 2 marks

    Suppose s = '15'. Which of the following expressions evaluates to True without raising an error?

    1. s > 9

    2. s > '9'

    3. float(s) == '15.0'

    4. s == 15

    5. int(s) > 9

    Show solution

    Answer: (E)

    (E). (A) raises a TypeError because it compares a str with an int. (B) compares strings character by character, and '1' < '9', so it is False. (C) and (D) compare a number with a string, which is always False.

  3. For and While Loops · Complete the code · 2 marks

    Fill in the blanks to complete the function. Do not change any other code.

    def first_word(sentence: str) -> str:
        """Return the characters of sentence up to but not including its
        first space. If sentence has no space, return all of sentence.
    
        >>> first_word('library books are due')
        'library'
        >>> first_word('overdue')
        'overdue'
        """
        i = 0
        while ____________________________________________:
            i = i + 1
        return ________________________
    Show solution
        while i < len(sentence) and sentence[i] != ' ':
            i = i + 1
        return sentence[:i]

    The bound check i < len(sentence) must come first; with the operands swapped, first_word('overdue') raises an IndexError.

  4. Nested Lists and Loops · Write code · 3 marks

    A transit agency counts how many people board each bus trip at each stop: riders[t][s] is the number of people who boarded trip t at stop s. Complete the function body according to its docstring.

    def busiest_stop(riders: list[list[int]]) -> int:
        """Return the index of the stop with the most boardings over all
        trips. If several stops tie, return the smallest such index.
    
        Precondition: riders is not empty, all of its rows have the same
        non-zero length, and every item is >= 0.
    
        >>> busiest_stop([[4, 10, 2], [6, 1, 9], [5, 0, 7]])
        2
        >>> busiest_stop([[1, 2], [2, 1]])
        0
        """
    Show solution
        best_stop = 0
        best_total = -1
        for s in range(len(riders[0])):
            total = 0
            for trip in riders:
                total = total + trip[s]
            if total > best_total:
                best_stop = s
                best_total = total
        return best_stop

    Common mistake: summing rows (trips) instead of columns (stops), or using >= so that ties return the largest index.

  5. Searching, Sorting and Complexity · Short answer · 2 marks

    For each situation, say whether you would use linear search or binary search, and give a one-sentence reason.

    1. A list of 80,000 library book IDs is kept sorted. The checkout desk searches it thousands of times every day. [1]

    2. A list of the 300 card transactions made today, in the order they happened, is searched once at the end of the day for a particular amount. [1]

    Show solution

    (a) Binary search: the list is already sorted and each search takes about 17 iterations instead of up to 80,000.
    (b) Linear search: the list is not sorted, and sorting it just to do one search costs more than one linear pass. Binary search on the unsorted list would give wrong answers.

About ESC180H1 practice exams

ESC180H1 practice exams

Introduction to Computer Programming · University of Toronto (UofT, U of T, UToronto) · also written ESC180, ESC 180

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

Looking for ESC180H1 past exams?

Start with the past exams and tests your instructor or the UofT library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh ESC180H1 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
  • Tuples and Dictionaries · 100 questions
  • Searching, Sorting and Complexity · 100 questions

More UofT courses

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