- Define: [3]
- Bit: smallest unit of information, either a 1 or a 0, true/false, high/low, etc.
- Nibble: 4 bits, one hexadecimal digit
- Byte: 8 bits, can represent an ASCII character
- Word: unit of data the CPU works on, often 32bits or 64bits
- Kilobit: either 210 bits (binary units)
or 103 bits (SI units)
- Convert 473 (decimal) to binary, octal, and hexadecimal. [4]
111011001, 0731, 0x1d9
- Come up with a creative application for Python dictionaries.
What would the keys represent? What would the values represent? [2]
- Create a sample dictionary to illustrate your application. [1]
appleInv = { 'Fuji':3, 'Gala':5, 'Pacific Rose':12 }
- Write a Python code snippet to print the contents (keys and values) of your
dictionary in a nicely formatted way. [3]
foreach key in appleInv.keys():
print "You have", appleInv[key], key, "apples!"
-
In your own words, contrast the procedural vs.
object-oriented programming paradigms. [4]
Procedural programming is action-oriented: the code is a sequence of
procedures, and the data structures are passed as parameters to the procedures.
The focus is on the 'verbs'. In contrast, object-oriented programming is
data-oriented: code is organized into classes, centred around objects, and the
procedures are treated as methods within each class. The focus is on the
'nouns'.
-
Describe the cylinder/head/sector geometry of hard drives. [3]
Hard drives have one or more flat annular platters.
The cylinder number refers to radius away from the centre of the platter.
The position of the arm on which the read/write head is mounted determines
the cylinder.
The head number refers to which platter and which side of the platter (each
platter has two heads, one for each side).
The sector number refers to an angular rotation of the platters. As the platters
spin, different sectors pass under the heads.
-
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
myFav.
Let yrFav be the set containing Fuji and Melba.
- Find the set union of myFav and yrFav.
Express in words the interpretation of this
in the context of apple preferences. [2]
{ Fuji, Gala, Rome, Melba }.
These are the varieties that either you or I like.
- Find the symmetric set difference yrFav ^ myFav.
Express in words the interpretation of this
in the context of apple preferences. [2]
{ Gala, Rome, Melba }.
Those varieties that exactly one of us (but not both) likes
- We wish to implement some set operations using bitsets in Python.
Initialize five Python variables Fuji, Gala, Spartan, Rome, Melba
with appropriate integer values for use in a bitset.
[2]
fuji = 1 << 0 # or fuji = 1
gala = 1 << 1 # or gala = 2
spartan = 1 << 2 # or spartan = 4
rome = 1 << 3 # etc.... you could also use a different ordering
melba = 1 << 4
- Using the variables you just defined, define a Python bitset
for myFav. [1]
myFav = fuji | Gala | Rome
- Using the values you assigned for each entry in the bitset, evaluate
your preceding expression to get a number for myFav. [1]
myFav = 11 # using the values given above
- Write a Python expression for the intersection of
myFav and yrFav. [1]
myFav & yrFav
-
In the following block of Python code, list all the namespaces
present anywhere in the code. Draw boxes (where applicable) to indicate
the scope of each namespace. In each box, list all the non-builtin
names that are visible in that scope. (Hint: variables aren't
the only entities that have names!) [6]
a = 1
def fun(b):
from math import sqrt
c = sqrt(b)
class Glass:
d = 2
def __init__(self, e=3):
f = 4
self.g = 5
Namespaces:
(1) Default (everywhere): a, fun, Glass
(2) Global (everywhere in this file): a, fun, Glass
(3) Local to fun(): a, fun, Glass, b, sqrt, c
(4) Local to Glass: a, fun, Glass, d, __init__
(5) Local to __init__(): a, fun, Glass, d, self, e, f, self.g
-
Tell me everything you know about the global keyword in Python.
What is it used for? What will happen if we don't use it when we ought to?
[3]
global indicates which variables from an enclosing scope we'd
like write access to in this scope. If we don't use it, by
by default Python protects us from modifying global variables
(unintended side-effects). Without global, any assignment to
an identifier not in the local scope results in modifying a copy that
"masks" the original. The variable in the enclosing scope is not modified.
Using a global declaration tells Python that we do want to modify
the variable in the enclosing scope.
-
Define or describe each of the following object-oriented terms:
[8]
- Class: a user-defined container type
- Instance: an object; instances are to classes as variables are to
types
- Attribute: a variable or method belonging to an object
- Method: a function/procedure belonging to an object
- Constructor/Initializer: the special method which is called when an object is
instantiated
- Message: communication between objects; how methods are called
- 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
-
Assume the file input.txt contains a list of integers, one per line.
Write a Python program to read in the integers, and write their average at the
end of the file. Docstring and comments are not required. Use a separate sheet
of paper if necessary.
[6]
sum = 0
numEntries = 0
myFile = open('input.txt')
while (line = myFile.readline()):
sum += int(line)
numEntries += 1
myFile.close()
myFile = open('input.txt', 'a')
myFile.write("" + 1.0 * sum / numEntries)
myFile.close()
-
Recall that input() evaluates what the user types as a Python
expression and returns the result. Hence, if a user doesn't type anything
and simply presses "Enter", input() fails to evaluate the expression
and throws a SyntaxError: "unexpected EOF while parsing".
Write a block of Python code that uses
input() to get input from the
user, but doesn't crash if the user only presses "Enter". [3]
Additionally, print a thank-you message to the user afterward,
but only if no error was raised, and only if the user didn't only
press "Enter". [2]
input() may still raise other errors. Ensure that a graceful
farewell is always printed no matter what happens. [2]
try:
x = input("Please type a message: ")
except SyntaxError:
print "Please make sure you type something."
else:
print "Thank you!"
finally:
print "OK, we're done; thanks for coming!"
-
- (revised) Explain the differences among alias,
shallow copy, and deep copy; use diagrams as appropriate.
[3]