Sample Midterm #2

These are sample questions that are very similar to the ones I will ask on the midterm. The actual midterm will be about this length.

  1. Evaluate each expression. If any cannot be evaluated, say why.
    1. "hello"+ "goodbye"
    2. 7 * True
    3. 6 / "spam"
    4. True and (not False or True)
    5. 'she' in "hehisandits"
  2. Convert the following into Python.
    1. “hello” follows “goodbye” in the character ordering of the computer
    2. The value of the variable answer is true or the value of the variable y is less than 126
    3. The function gleep is called with an argument of −21
  3. Convert the following into a “for” loop.
    i = 1
    while i < 10:
        print i * "x"
        i += 3
  4. What does the following function do when given a string as the first argument and a string containing only one character as the second argument?
    def x(s, c):
        i = 0
        while i < len(s):
            if s[i] == c:
                return i
            i += 1
        return -1
  5. What does the following function do?
    def a(s):
        if len(s) < 2:
            return s
        return s[ -1] + a(s [1: -1]) + s[0]
  6. What does the following program do?
    d = dict ()
    while True:
        try:
            line = input ("EOF to stop > ")
        except EOFError:
            break
        for i in line:
        d[i] = d.get(i, 0) + 1
    u = d.keys()
    for i in u:
        print(i, d[i])

A PDF version is available here.
ECS 10, Basic Concepts of Computing
Winter Quarter 2012