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

Total points: 50
  1. Describe the five software flow/control abstractions mentioned in class. [3]

    _Sequence__________ _Selection__________ _Repetition__________ _Composition__________ _Parallelism__________
  2. 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.) [3]
    _Write__________ _Apprehend__________ _Design__________ _Execute__________ _Scrutinize__________


  3. Show the output of the following Python loop: [4]
    for i in range(2):
    	for j in range(3):
    		print "(%d,%d)" % (i, j),
    	print
    

    (0,0) (0,1) (0,2)
    (1,0) (1,1) (1,2)


  4. Show the output of the following Python loop: [4]
    radius = 1
    pi = 3.0			# just to make the math easier!
    while radius <= 10:
    	area = pi * radius**2
    	if radius == 4:
    		continue
    	print "r=%d: a=%.2f" % (radius, area)
    

    Infinite loop! radius never incremented. Prints "r=1: a=3.00" forever
  5. Show the output of the following Python loop: [4]
    i = 0
    while i < 10:
    	print i,
    	if i**2 > 10:
    		break
    	i += 1
    else:
    	print "done!"
    

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

  7. Compare and contrast static typing versus dynamic typing. What are pros/cons of each? [4]

    (The key here is type: not whether the values of the variables can change, but whether their types can change.) Static typing: variables cannot change type Must declare type of variable and initialize value Compiler enforces correct type Dynamic typing: can change type More flexible, but usually we don't want variables to change type

  8. Write "yes" or "no" next to each of the following six strings to indicate whether it can be used as a valid name for a user-defined Python variable. If "no", indicate why. [6]
    in no (reserved)______ 1st_place no (starts with digit)______ FALSE yes______
    twu@seanho no (punctuation)______ IF yes______ fill_in_the.blankno (punctuation)______


  9. 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. For all expressions, assume the following initialization:[10]
    	myApple = "Fuji"	
    1. len(myApple) = 3 SyntaxError: can't assign
    2. 2.0 + 14 / 4 5.0
    3. 7 / 4<1.5 or (5/0 < 1) True
    4. "%04d%s" % (15.62, 'apples') '0015apples'
    5. "%03.2f" % 3.1415926 '3.14'
    6. range(len(myApple)) [0, 1, 2, 3]
    7. string.upper(myApple[1]) 'U'
    8. ord('f') - ord('d') 2
    9. 'f' + 'd' 'fd'
    10. 'f' - 'd' TypeError: can't subtract string



  10. Write a Python function sum_squares() that returns the sum of the first n squares -- e.g., sum_squares(4) returns the sum of 1+4+9+16. [9]

    Docstrings, including pre- and post-conditions, are required on this exam! Comments are not required, however if your code is incorrect, your comments may earn you partial credit if they show good design thinking. Little partial credit will be given for uncommented incorrect code.
    See sumsquares.py.