Sample CS 116 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.

    score = 87
    print('Score: ' + str(score) + '%')
    print('Score:', score, '%')
    print('Score:', str(score) + '%', sep='')
    Show solution
    Score: 87%
    Score: 87 %
    Score:87%

    + joins strings with nothing in between; commas in print insert sep (a space by default).

  2. Booleans and Conditionals · Write code · 2 marks

    Complete the function body according to its docstring.

    def triangle_type(a: int, b: int, c: int) -> str:
        """Return 'equilateral' if the side lengths a, b and c are all
        equal, 'isosceles' if exactly two of them are equal, and
        'scalene' if no two of them are equal.
    
        Precondition: a, b and c are the sides of a valid triangle.
    
        >>> triangle_type(3, 3, 3)
        'equilateral'
        >>> triangle_type(5, 3, 5)
        'isosceles'
        >>> triangle_type(3, 4, 5)
        'scalene'
        """
    Show solution
        if a == b == c:
            return 'equilateral'
        elif a == b or b == c or a == c:
            return 'isosceles'
        else:
            return 'scalene'

    Checking the equilateral case first matters: an equilateral triangle also satisfies the isosceles condition. Forgetting the a == c comparison is a common mistake.

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

    Consider the code below.

    L = [1, 2]
    w = id(L)
    L.append(3)
    x = id(L)
    L = L + [4]
    y = id(L)
    L[0] = 10
    z = id(L)

    Circle ALL expressions that evaluate to True after this code runs.

    1. w == x

    2. x == y

    3. y == z

    4. w == z

    Show solution

    Answer: A, C

    append and item assignment mutate the existing list, so the id is unchanged. L + [4] creates a new list object, so L refers to a different id from then on.

  4. Tuples and Dictionaries · Predict the output · 2 marks

    Write the output of the code below.

    def order(a: int, b: int) -> tuple[int, int]:
        if a > b:
            return b, a
        return a, b
    
    pairs = [(5, 2), (1, 9)]
    for p in pairs:
        low, high = order(p[0], p[1])
        print(low, high)
    x = order(4, 4)
    print(x, x[0] == x[1])
    Show solution
    2 5
    1 9
    (4, 4) True
  5. Classes and Objects · Design · 4 marks

    A colour on a screen has red, green and blue components, each an int from 0 to 255. Write class Colour. Every method needs a header with a type contract and a docstring.

    1. Write the class header, the class docstring and the complete __init__ method, which takes the three components. Include a precondition. [2]

    2. Write a complete method __str__ that returns a string like 'rgb(255, 128, 0)'. [1]

    3. Write a complete method __eq__. Two colours are equal when all three components are equal. [1]

    Show solution
    class Colour:
        """A colour made of red, green and blue components."""
    
        def __init__(self, red: int, green: int, blue: int) -> None:
            """Initialize a colour with the given components.
    
            Precondition: each of red, green and blue is in 0..255
            """
            self.red = red
            self.green = green
            self.blue = blue
    
        def __str__(self) -> str:
            """Return this colour in the form rgb(R, G, B)."""
            return ('rgb(' + str(self.red) + ', ' + str(self.green) + ', '
                    + str(self.blue) + ')')
    
        def __eq__(self, other: 'Colour') -> bool:
            """Return True if and only if this colour and other are equal."""
            return (self.red == other.red and self.green == other.green
                    and self.blue == other.blue)
About CS 116 practice exams

CS 116 practice exams

Introduction to Computer Science 2 · University of Waterloo (Waterloo, UWaterloo, UW) · also written CS116

Make a free, printable CS 116 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 CS 116 past exams?

Start with the past exams and tests your instructor or the Waterloo library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh CS 116 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
  • For and While Loops · 100 questions
  • Lists, Mutability and Aliasing · 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 Waterloo courses

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