-
Describe the five software flow/control
abstractions mentioned in class. [3]
_Sequence__________ |
_Selection__________ |
_Repetition__________ |
_Composition__________ |
_Parallelism__________ |
-
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__________ |
-
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)
-
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
-
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
-
Use the Python range() function to create this list:
[7, 5, 3, 1, -1]. [3]
range(7,-2,-2) or range(7,-3,-2)
-
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
-
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.blank | no (punctuation)______ |
-
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"
- len(myApple) = 3 SyntaxError: can't assign
- 2.0 + 14 / 4 5.0
- 7 / 4<1.5 or (5/0 < 1) True
- "%04d%s" % (15.62, 'apples') '0015apples'
- "%03.2f" % 3.1415926 '3.14'
- range(len(myApple)) [0, 1, 2, 3]
- string.upper(myApple[1]) 'U'
- ord('f') - ord('d') 2
- 'f' + 'd' 'fd'
- 'f' - 'd' TypeError: can't subtract string
-
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.