Sample CSC148H1 questions
  1. Classes, Encapsulation and Representation Invariants · Multiple choice · 1 mark

    A class has the representation invariant self.total == sum(self._scores). Which statement about this invariant is correct?

    1. It may be false in the middle of a method, as long as it is true again when the method returns.

    2. It must be true at every moment, including the middle of a method.

    3. It only has to be true right after __init__ finishes.

    4. It only applies to methods that change self._scores.

    Show solution

    Answer: (A)

    (A). A method that appends a score and then updates the total is momentarily inconsistent between the two statements. What matters is that the invariant holds whenever no method of the object is running: after __init__ and at the end of every method.

  2. Stacks, Queues and Exceptions · Predict the output · 2 marks

    Write the output of the code below.

    class Overflow(Exception):
        pass
    
    def load(items: list[int], limit: int) -> Stack:
        s = Stack()
        count = 0
        for x in items:
            if count == limit:
                raise Overflow
            s.push(x)
            count = count + 1
        return s
    
    try:
        a = load([1, 2, 3], 3)
        print('a ok')
        b = load([1, 2, 3, 4], 3)
        print('b ok')
    except Overflow:
        print('overflow')
    print(a.pop())
    Show solution
    a ok
    overflow
    3

    a is loaded successfully. In the second call the fourth item hits the limit and Overflow is raised before the function returns, so the assignment to b never happens and b ok is not printed. a is still defined, so a.pop() returns 3.

  3. Linked Lists · Short answer · 2 marks

    A LinkedList has no size attribute. Let nn be its number of items. State the worst-case running time of a __len__ method that counts the nodes, and explain in one sentence why Python’s len on a built-in list is faster.

    Show solution

    O(n)O(n): every node must be visited once. A built-in list stores its length, so len is O(1)O(1).

  4. Trees · Multiple choice · 2 marks

    Every node of a tree has at most 3 children, and the tree has height 4. What is the largest possible number of items it can contain?

    1. 12

    2. 27

    3. 40

    4. 81

    Show solution

    Answer: (C)

    (C). A full tree has 1+3+9+27=401 + 3 + 9 + 27 = 40 items at depths 0, 1, 2, 3. (D) is 343^4, which forgets that the levels must be added.

  5. Recursive Sorting · Complete the code · 2 marks

    This version of mergesort sorts the part lst[lo:hi] of a list, and uses the course merge (given). Fill in the four blanks.

    def mergesort_range(lst: list, lo: int, hi: int) -> None:
        """Sort lst[lo:hi] in place (hi is excluded); other items are unchanged.
        """
        if hi - lo < 2:
            return
        mid = (lo + hi) // 2
        mergesort_range(lst, lo, ________)
        mergesort_range(lst, ________, hi)
        lst[lo:hi] = merge(lst[lo:________], lst[________:hi])
    Show solution
        mergesort_range(lst, lo, mid)
        mergesort_range(lst, mid, hi)
        lst[lo:hi] = merge(lst[lo:mid], lst[mid:hi])

    All four blanks are mid. The slice assignment lst[lo:hi] = ... changes lst itself, unlike lst = ..., which would only rebind the local name.

About CSC148H1 practice exams

CSC148H1 practice exams

Introduction to Computer Science · University of Toronto (UofT, U of T, UToronto) · also written CSC148, CSC 148

Make a free, printable CSC148H1 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 CSC148H1 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 CSC148H1 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

  • Classes, Encapsulation and Representation Invariants · 100 questions
  • Inheritance, Abstract Classes and Polymorphism · 100 questions
  • Stacks, Queues and Exceptions · 100 questions
  • Running Time and Big-Oh · 100 questions
  • Linked Lists · 100 questions
  • Recursion and Nested Lists · 100 questions
  • Trees · 100 questions
  • Binary Search Trees · 100 questions
  • Recursive Sorting · 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.