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

Total points: 70
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.)
Also: describe each step in your own words, and why it is important! 
• Write
• Apprehend
• Design
• Execute
• Scrutinize
2. Name the five computer hardware abstractions we talked about in class. For each one, name one computer-related example, and also describe one non-computer-related example of the abstraction. 
• Input: keyboard; steering wheel
• Storage: RAM; odometre
• Processing: CPU; ECU
• Control: chipset; suspension/supporting systems
• Output: screen; wheels
3. Show the output of the following Python loop: 
```distance = 0.8
while True:
print "%.1f" % distance,		# in Py3: print("%.1f" % distance, end=' ')
distance -= 0.1
if distance == 0.0:
print "Boom!"			# in Py3: print("Boom!")
break
```
Infinite loop! Due to inaccuracy in floating-point, distance may miss the sentinel value 0.0 and keep going negative, forever!
4. 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. 

four -- yes
for -- no (reserved)
3D_Gaming -- no (starts with digit)

wagesIn\$USD -- no (punctuation)
__0__ -- yes
In-N-Out -- no (punctuation)
5. The following is meant to be a guessing game that quits only when the user types the magic number ("42"). Find all the bugs in the code, and fix them. 

```while true:
guess == raw_input('Bet you can't guess my number! ')
if guess > 42:
print 'Sorry, too high!'
else if guess < 42:
print 'Sorry, too low!'
# guess = 42
print 'You got it!';
continue
print "Bye bye!"
```
(In Py3):
```while true:
guess == input('Bet you can't guess my number! ')
if guess > 42:
print('Sorry, too high!')
else if guess < 42:
print('Sorry, too low!')
# guess = 42
print('You got it!');
continue
print("Bye bye!")
```
(1) True;
(2) guess =, not guess ==
(3) input(), not raw_input()
(4) Double-quotes to protect "can't"
(5) elif, not else if
(6) Last two lines should be in an else clause
(7) Delete semicolon at end of 'You got it!'
(8) Use break instead of continue

6. Use the Python range() function to create the following list.
(There are several possible correct answers.)
[-8, -3, 2, 7, 12]. 
range(-8, 13, 5), or ..., up to range(-8, 17, 5)
(in Py3: the above would be acceptable, or list(range(...)).)

7. Compare and contrast static typing versus dynamic typing. What are pros/cons of each? 
(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. Compare and contrast C arrays and Python lists. What are the limitations of arrays as compared with Python lists? 
Python lists can change length (can insert/delete), can have items of differing type, can have items which change type (due to Python's dynamic typing), and have various operators/functions defined on them, such as del, len(), negative indexing, list slicing, etc.

9. Compare and contrast Call-by-value versus call-by-reference. Describe a situation in which you might want to use call-by-reference instead of call-by-value. 

10. 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 only the following initialization: 
`	myApple = "McIntosh"	`
1. myApple[2:4].upper() 'IN'
2. list(range(5,5)) [] (empty list)
3. 10 - 21 / 2 / 3 7 (in Py3: 6.5)
4. 4 % 2 0
5. "04d" % 2 TypeError: mismatch in arguments
6. "1%04d.%s" % (23.92, 'jpg') '10023.jpg'
7. "%06.3f" % 3.1415926 '03.142'
8. math.pi > 5 and 12%0 == 0 False
9. len(myApple) 8
10. len(myApple) /= 7 or len(myApple) /= 9 SyntaxError: can't assign
11. I in myApple NameError: I is not a variable
12. chr(ord('d') + 3) 'g'
13. ord('f') - ord('d') 2
14. 'f' * 'd' TypeError: can't multiply string
15. 2*"x" + 3*"y" + "z" 'xxyyyz'
11. Write a Python function harmonic(n) that returns the mathematical sum
1 + 1/2 + 1/3 + 1/4 + ... + 1/n. (This is called the harmonic series.) 

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