Sample CSC108H1 questions
  1. Expressions, Types and Memory · Trace the code · 3 marks

    Assume x = 7, y = 2 and z = 3.0 have been run. Give the value of each expression, showing float values with a decimal point.

    ExpressionValue
    x // y   
    x % y   
    x // z   
    x % z   
    y ** z   
    (x + y) // z   
    Show solution

    Answer: 3; 1; 2.0; 1.0; 8.0; 3.0

    See the completed table above. Whenever one operand is a float, //, % and ** also give a float.

  2. Booleans and Conditionals · Predict the output · 2 marks

    Write the output of the following code.

    def check(n: int) -> str:
        if n > 10:
            return 'big'
        elif n > 5:
            print('medium')
    
    print(check(12))
    print(check(7))
    print(check(2))
    Show solution
    big
    medium
    None
    None

    check(7) prints medium inside the function and then returns None, which the outer print displays. check(2) runs no branch and also returns None.

  3. Lists, Mutability and Aliasing · Find the bug · 4 marks

    The function below does not work as its docstring says.

    def sorted_copy(values: list[int]) -> list[int]:
        """Return a new list containing the items of values in
        non-decreasing order. values is not changed.
    
        >>> nums = [4, 1, 3]
        >>> sorted_copy(nums)
        [1, 3, 4]
        >>> nums
        [4, 1, 3]
        """
        result = values
        result = result.sort()
        return result
    1. Suppose nums = [4, 1, 3]. What does sorted_copy(nums) return, and what is the value of nums after the call? [2]

    2. Rewrite lines 11–13 so that the function is correct. [2]

    Show solution
    1. It returns None (sort returns None), and nums becomes [1, 3, 4] because result is an alias of values, so the list passed in is sorted.

    2.     result = values[:]
          result.sort()
          return result
  4. Tuples and Dictionaries · Complete the code · 2 marks

    Consider this function:

    def mystery(d: dict[str, int]) -> list[str]:
        result = []
        for k in d:
            if d[k] > len(k):
                result.append(k)
        return result
    1. Complete the docstring example by filling in the return value. [1]

      >>> mystery({'sun': 4, 'a': 1, 'rain': 9, 'fog': 2})
    2. Give a dictionary with exactly three keys for which mystery returns ['ab']. [1]

    Show solution
    1. ['sun', 'rain']

    2. Any dictionary with keys 'ab' and two others, where only 'ab' maps to a number greater than its length, e.g. {'ab': 5, 'c': 0, 'xyz': 3}. ('a': 1 does not count: 1 is not greater than 1.)

  5. Classes and Objects · Predict the output · 3 marks

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

    class Box:
        """A labelled storage box."""
    
        def __init__(self, label: str) -> None:
            self.label = label
    
    def relabel(box: Box, label: str) -> None:
        box.label = label
    
    def replace(box: Box, label: str) -> None:
        box = Box(label)
        box.label = box.label.upper()
    
    b1 = Box('books')
    b2 = b1
    relabel(b2, 'toys')
    print(b1.label)
    replace(b1, 'tools')
    print(b1.label, b2.label)
    b2 = Box('maps')
    print(b1.label, b2.label, b1 is b2)
    Show solution
    toys
    toys toys
    toys maps False

    relabel mutates the object both variables refer to. In replace, box = Box(label) only rebinds the local variable, so the caller’s box is unchanged.

About CSC108H1 practice exams

CSC108H1 practice exams

Introduction to Computer Programming · University of Toronto (UofT, U of T, UToronto) · also written CSC108, CSC 108

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

Looking for CSC108H1 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 CSC108H1 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
  • Searching, Sorting and Complexity · 100 questions
  • Classes and Objects · 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.