Sample CMPUT 174 questions
  1. Expressions, Types and Memory · Multiple choice · 1 mark

    What is printed by the following code?

    print('A', 'B', sep='', end='')
    print('C', 'D', end='!')
    1. ABC D!

    2. AB on one line, then C D! on the next line

    3. ABCD!

    4. A B C D!

    5. ABC!D!

    Show solution

    Answer: (A)

    ABC D!. The first call prints AB with no newline; the second uses the default sep (a space) between C and D, then ends with !.

  2. Booleans and Conditionals · Testing · 3 marks

    Consider this function header and docstring.

    def rating_word(stars: int) -> str:
        """Return 'poor' for 1 or 2 stars, 'average' for 3 stars,
        and 'good' for 4 or 5 stars.
    
        Precondition: 1 <= stars <= 5
        """

    A student tests their implementation with stars equal to 1, 3 and 5, and all three tests pass.

    1. Write a body for rating_word that passes all three of these tests but is still incorrect. [2]

    2. Which set of test cases would you choose for this function, and why? [1]

    Show solution

    (a) For example (wrong for 2 stars):

        if stars < 2:
            return 'poor'
        elif stars < 4:
            return 'average'
        else:
            return 'good'

    (b) Test all five values 1 to 5: there are only five valid arguments, and 2 and 4 are the boundaries between categories that the student skipped.

  3. For and While Loops · Predict the output · 2 marks

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

    1. result = ''
      for ch in 'stressed':
          result = ch + result
      print(result)

      [1]

    2. s = 'banana'
      result = ''
      for i in range(len(s)):
          if i % 2 == 0:
              result = result + s[i].upper()
          else:
              result = result + s[i]
      print(result)

      [1]

    Show solution

    (a)

    desserts

    (b)

    BaNaNa
  4. Nested Lists and Loops · Predict the output · 3 marks

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

    teams = [[3, 'Lynx'], [5, 'Owls'], [3, 'Bees'], [5, 'Ants']]
    print(max(teams))
    teams.sort()
    print(teams[0])
    print(teams[-1][1])
    print(teams.index([5, 'Ants']))
    print(min(teams[1]))
    Show solution
    [5, 'Owls']
    [3, 'Bees']
    Owls
    2
    ERROR

    Lists are compared item by item: first by the number, then (on a tie) by the name. The last line tries to compare an int with a str, which raises a TypeError.

  5. Testing and Debugging · Find the bug · 3 marks

    The function below has a bug.

    def clean_title(title: str) -> str:
        """Return title with each underscore replaced by a space and with
        leading and trailing spaces removed.
    
        >>> clean_title('  war_and_peace ')
        'war and peace'
        """
        title.replace('_', ' ')
        title.strip()
        return title
    1. What does the docstring example actually return? [1]

    2. Explain the bug in one sentence. [1]

    3. Rewrite the body so that the function is correct. [1]

    Show solution
    1. ' war_and_peace ' (the argument, unchanged).

    2. Strings are immutable: replace and strip return new strings, and the code throws those results away.

    3. Body:

          title = title.replace('_', ' ')
          title = title.strip()
          return title
About CMPUT 174 practice exams

CMPUT 174 practice exams

Introduction to the Foundations of Computation I · University of Alberta (UAlberta, UofA, U of A) · also written CMPUT174

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

Start with the past exams and tests your instructor or the UAlberta library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh CMPUT 174 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
  • Testing and Debugging · 100 questions

More UAlberta courses

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