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.)
- Fill in the blank: 
- "Computers are tools, and computing scientists are
- How many bits are in a nibble? 4
- 1011 0110 shifted two right, in binary, is 101101
- Convert 0x1BE to decimal: 446
- Convert 1100 0110 from binary to octal: 0306
- What word describes functions which invoke themselves?
- The Python command used to trigger a user-defined exception is
- False : The word Apples_For_Dr.Ho
is a legal Python identifier.
- What is the type of the Python expression 5**2 ? int
- What Python command is used to convert an integer into a real value?
- Name the two optional reserved words used in the Python program control
construct which corresponds to selection: elif, else
- The first assignment of a value to a variable is called
- A cable modem connection runs at 4Mb/s. Assuming full utilization of
the bandwidth (which never happens in reality), how long would it take to
download a 2GB file? Use binary units. 4096 seconds
- The standard Python function which produces a pseudorandom real number
between 0 and 1 is in the
random standard library and is named random().
- The Python function which counts the number of characters in a string
is named len().
- What English part of speech should a function name be?
- What symbol is used to separate statements in a sequence in C or M2?
- What symbol is used to indicate logical negation in Python?
- 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
Name the five computer program flow abstractions described in the text,
and for the four which we covered in class, show how each is done in Python.
- Sequence: newline
- Selection: if/elif/else
- Repetition: while/for
- Composition: def (functions)
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: 
myPear = "DAnjou"
from math import pi
- 'pi' + 2*'z' + 'a' 'pizza'
- 2 + 1>4 and (5/0 < 1) False
- 2.0 + 14 / 4 5.0
- 6 ^ 5 3
- "%03d%s" % (15.189, 'pears') '015pears'
- string.upper(DAnjou) NameError: DAnjou
- "%05.2f" % 2.7182818 '02.72'
- '%.2f' % (19 % 5) '4.00'
- range(myPear) TypeError
- myPear[1:4] 'Anj'
- 'd' + 'b' 'db'
- ord('d') - ord('b') 2
- 'd' - 'b' TypeError
- 'b' + 3 == 'd' TypeError
What is a namespace? Tell me everything you know about scope rules and
namespaces in Python.
A namespace is a mapping (dictionary) from identifiers (names) to objects
(values). There are three kinds of namespaces: the local namespace within a
function/class/block, the global namespace for each module/file, and the
default built-in namespace for all of Python. The scoping rules resolve names
by moving outward from the local namespace, to any enclosing function/class
definitions, to the global namespace for the file, to the default namespace.
An orchard grows five apple varieties:
Fuji, Gala, Spartan, Rome, and Melba.
The apples I like are Fuji, Gala, and Rome; let this set be denoted by
Let yrFav be the set containing Fuji and Melba.
- Consider the intersection of myFav and yrFav.
Express in words the interpretation of this intersection
in the context of apple preferences. 
The intersection of myFav and yrFav is the set of apple varieties that
both you and I like.
- Consider the set difference yrFav - myFav.
Express in words the interpretation of this set difference
in the context of apple preferences. 
The set difference yrFav - myFav is the set of apple varieties that
you like but that I don't like
- We wish to implement some set operations using bitsets in Python.
Define five Python variables Fuji, Gala, Spartan, Rome, Melba
with appropriate integer values for use in a bitset.
fuji = 1 << 0
gala = 1 << 1
spartan = 1 << 2
rome = 1 << 3
melba = 1 << 4
- Using the variables you just defined, define a Python bitset
for yrFav. 
myFav = fuji | melba
- Write a Python expression for the intersection of
myFav and yrFav. 
myFav & yrFav
What is wrong with the following Python loop? Rewrite it from scratch
in a better way. 
counter = 0
sum = 0.0
numitems = 0
while counter < len(mylist):
sum = sum + mylist[counter]
numitems = numitems + 1
average = sum / numitems
counter is never incremented. Use a for loop:
sum = 0
for item in mylist:
sum += item
average = sum / len(mylist)
The following Python function has at least eleven errors. Find and fix all of
them. Points will be deducted for "fixing" what are not errors.
"""Round a float to the nearest integer (up or down)."""
addhalf := (float++ 0.5) // add 0.5 to float
Define or describe each of the following object-oriented terms:
- Class: a user-defined container type
- Instance: an object; instances are to classes as variables are to
- Attribute: a variable or method belonging to an object
- Method: a function/procedure belonging to an object
- Constructor: the special method which is called when an object is
- Interface: a set of messages that an object can receive
- Overloading: giving multiple definitions to a
function/method/operator depending on the type of its parameters/operands
Modula-2 and C have no built-in string type. What is a string
in Modula-2 or C?
In M2 and C, strings are just arrays of characters, terminated by a
Describe all the differences between Python lists and C arrays.
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.
Explain all the differences between atomic and
aggregate/compound/container data types. Give examples of each.
Atomic data types represent just a single entity/value: e.g., int, float,
bool, complex. Container types represent a collection of items: e.g., list,
tuple, dict, string, set, class.
The file 'input.txt' contains a list of real numbers, one per line.
Write a Python program to read in the numbers from the file and print
their average to the screen. Docstring is required; pseudocode is not.
"""Read in numbers from a file and print their average.
The file 'input.txt' must contain float numbers, one to a line, no blank lines."""
fileHandle = open('input.txt', 'r')
stringList = fileHandle.readlines()
sum = 0.0
for s in stringList:
sum += float(s)
print "The average is", sum/len(stringList)
A certain sequence is defined by t1=0, t2=3, and
after that ti=ti-1 + 2 ti-2, so that
the first few terms of the sequence are 0, 3, 3, 9, 15, 33, 63, ....
On a separate paper, write a recursive Python function to compute
the nth term of this
sequence. Docstring and preconditions are required; pseudocode is not (but may
be good for partial credit).
"""Calculate the nth term of this funky sequence."""
if n <= 1:
elif n == 2:
return sequence(n-2) + 2*sequence(n-3)
A dynamic system that is being accelerated at a constant acceleration
a has its initial velocity u, final velocity v, and
the distance travelled d related by the formula
v2 - u2 = 2ad.
On a separate paper,
write an interactive Python program that can calculate either
a in terms of the others. Use at least one function.
Docstrings are required for the program and all functions.
Pseudocode is not required (but may be good for partial credit).
"""Interactive velocity/distance calculator."""
def find_d(a, u, v):
"""Find the distance travelled, given accel and init/final velocities."""
return (v*v - u*u)/(2*a)
def find_a(d, u, v):
"""Find the accel, given distance and init/final velocities."""
return (v*v - u*u)/(2*d)
print "Welcome! Let's do some physics!"
u = float(input("What's the initial velocity? "))
v = float(input("What's the final velocity? "))
key = ''
while key != 'd' and key != 'a':
key = raw_input("Do you know the distance (d), or the acceleration (a)? ")
if key == 'd':
d = float(input("What's the distance travelled? "))
print "The acceleration is", find_a(d, u, v)
elif key == 'a':
a = float(input("What's the acceleration? "))
print "The distance travelled is", find_d(a, u, v)
print "Please type either 'd' or 'a'"
print "Hope you had fun!"