[ answers in web view ]
Name: _______________________________ K E Y
Student ID: _______________________________

Total points: 60
  1. Name the five steps to top-down problem solving as described in the text. (It's okay if you can't remember the exact wording; the concepts are more important.) [5]

    Write, Apprehend, Design, Execute, Scrutinize
    ___________ ___________ ___________ ___________ ___________
  2. Describe the five software control/flow abstractions mentioned in the text. For each one, also name a corresponding Python language construct. [5]

    Sequence (newlines in Python), Selection (if/elif/else), Repetition (while/for), Composition (def/functions), Parallelism
    ___________ ___________ ___________ ___________ ___________



  3. Fill in the blank: [13]
    1. "Computers are __________tools, and computing scientists are ______________toolsmiths."
    2. In statically-typed languages, before we can use the value of a variable for the first time in
      a program, we need to ______________declare and ________________initialize it.
    3. True or False (circle one): The word function is a legal Python identifier. True
    4. The Python method on file handles that ensures that all pending write()s have been committed to disk is called ________________ flush().
    5. The Python function which counts the number of characters in a string is ___________len().
    6. The standard function that converts strings to ALL-CAPS is found in the
      ___________string standard library and is named ____________upper().
    7. The standard output I/O channel is found in the
      ________sys standard library and is named ___________stdout.
    8. A procedure that invokes itself is called a ___________________recursive procedure.
    9. A requirement imposed by a function upon its parameters, such that the correctness of the result is not guaranteed if the requirement is not met, is called a __________________precondition / predicate.
  4. Recall that in Python int/float/bool parameters are essentially passed by value, and lists/dictionary parameters are essentially passed by reference. What is printed by the following block of Python code? [4]
    	def double_me(x):
    		x *= 2
    	y = 3
    	z = range(3)
    	double_me(y)
    	double_me(z)
    	print 'y =', y, ', z =', z
    
    y = 3, z = [0, 1, 2, 0, 1, 2]
    
    The variable y points to an immutable object (the float 3.0) and is roughly pass-by-value, so it is not affected by double_me(). The variable z points to a mutable object (the list range(3)) and is roughly pass-by-reference, so it is changed by double_me(). The real scoop on Python parameter passing is call-by-object and is a bit more complicated; it has to do with Python's object-oriented roots.



  5. Name three out of the five possible modes in which one can open a file in Python, and give example Python code for each. [5]
    read: open('file.txt', 'r')
    write: open('file.txt', 'w')
    read+write: open('file.txt', 'r+')
    append: open('file.txt', 'a')
    binary: open('file.txt', 'b')








  6. Use the Python range() function to create this list: [ 5, 3, 1, -1 ]. [3]
    range(5, -2, -2) or range(5, -3, -2)


  7. Assume that the variable numApples has integer type. Write a line of pseudocode that would work in a dynamically-typed language like Python but would fail in a statically-typed language like C or M2. [2]

    numApples = "Hello, World!", or numApples = 71.3, or numApples = False, etc.


  8. Evaluate each of the following Python expressions exactly as given, or if it produces an error, describe the error. Assume each expression is independent of the others. Assume all necessary imports have been done. [9]
    1. (5 % 12 == 2) or False False
    2. 2+2*2**2 10
    3. 3*'x' + 2*'y' + 'x' 'xxxyyx'
    4. not False or (12 % 0) True
    5. (3 > 2.9) and (15/3 = 5) SyntaxError (assign)
    6. "%s%04d.jpg" % ('img-', 99.9) 'img-0099.jpg'
    7. '%01.3f' % (16 % 0) ZeroDivisionError
    8. 2*range(3) [ 0, 1, 2, 0, 1, 2 ]
    9. chr(ord('H') + 2) 'J'

  9. What is output by the following block of Python code? [4]
    myList = [ ('a', 'b'), range(5), 'xyz', [ [2, 3, 4], 5] ]
    for item in myList:
    	print len(item),
    

    '2 5 3 2 '
  10. Tell me everything you know about libraries in the context of programming. [4]

    Libraries are containers/groups of functions and other entities provided for other programmers to use. A well-designed library will have a clear public interface (header or definition file) which tells the programmer what functionality the library provides and how to invoke its functions. The other component of the library is its implmentation, which is the code or bodies of its functions. The implmentation will often be hidden from the user. Some examples of standard Python libraries include math, sys, string, random. An example of a function provided by a library is math.sin(). An example of a non-function entity provided by a library is math.pi; it is a constant rather than a function.













  11. Write a Python function transpose() which takes a matrix (2D list) as a parameter and returns a new matrix, where each entry (i,j) of the new matrix is taken from entry (j,i) in the old matrix. Docstrings/comments not necessary but helpful for partial credit. [6]
    For example, the transpose of the matrix
    ab
    cd
    is the matrix
    ac
    bd

    def transpose(matrix):
    	"""Return the transpose of a matrix (2D list).
    	pre: m must be a rectangular 2D list, all rows of same length."""
    	numRows = len(matrix[0])
    	numCols = len(matrix)
    
    	tpose = [0] * numRows
    	for row in range(numRows):
    		tpose[row] = [0] * numCols
    		for col in range(numCols):
    			tpose[row][col] = matrix[col][row]
    	return tpose