-
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!
[5]
- Write
- Apprehend
- Design
- Execute
- Scrutinize
-
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.
[6]
- Input: keyboard; steering wheel
- Storage: RAM; odometre
- Processing: CPU; ECU
- Control: chipset; suspension/supporting systems
- Output: screen; wheels
-
Show the output of the following Python loop:
[4]
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!
-
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]
four -- yes
for -- no (reserved)
3D_Gaming -- no (starts with digit)
wagesIn$USD -- no (punctuation)
__0__ -- yes
In-N-Out -- no (punctuation)
-
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. [6]
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
-
Use the Python range() function to create the following list.
(There are several possible correct answers.)
[-8, -3, 2, 7, 12]. [3]
range(-8, 13, 5), or ..., up to range(-8, 17, 5)
(in Py3: the above would be acceptable, or list(range(...)).)
-
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.
-
Compare and contrast C arrays and Python lists.
What are the limitations of arrays as compared with Python lists?
[5]
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.
-
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.
[6]
-
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:
[15]
myApple = "McIntosh"
- myApple[2:4].upper() 'IN'
- list(range(5,5)) [] (empty list)
- 10 - 21 / 2 / 3 7 (in Py3: 6.5)
- 4 % 2 0
- "04d" % 2 TypeError: mismatch in arguments
- "1%04d.%s" % (23.92, 'jpg') '10023.jpg'
- "%06.3f" % 3.1415926 '03.142'
- math.pi > 5 and 12%0 == 0 False
- len(myApple) 8
- len(myApple) /= 7 or len(myApple) /= 9 SyntaxError: can't assign
- I in myApple NameError: I is not a variable
- chr(ord('d') + 3) 'g'
- ord('f') - ord('d') 2
- 'f' * 'd' TypeError: can't multiply string
- 2*"x" + 3*"y" + "z" 'xxyyyz'
-
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.)
[10]
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.