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

Total points: 62 (revised)
  1. Define: [3]
    1. Bit: smallest unit of information, either a 1 or a 0, true/false, high/low, etc.
    2. Nibble: 4 bits, one hexadecimal digit
    3. Byte: 8 bits, can represent an ASCII character
    4. Word: unit of data the CPU works on, often 32bits or 64bits
    5. Kilobit: either 210 bits (binary units) or 103 bits (SI units)
  2. Convert 473 (decimal) to binary, octal, and hexadecimal. [4]

    111011001, 0731, 0x1d9




    1. Come up with a creative application for Python dictionaries. What would the keys represent? What would the values represent? [2]


    2. Create a sample dictionary to illustrate your application. [1]
      	appleInv = { 'Fuji':3, 'Gala':5, 'Pacific Rose':12 }
        



    3. 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!"
        







  3. 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'.






  4. 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.







  5. 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.
    1. 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.


    2. 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


    3. 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
      





    4. Using the variables you just defined, define a Python bitset for myFav. [1]
        myFav = fuji | Gala | Rome
        


    5. 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
        


    6. Write a Python expression for the intersection of myFav and yrFav. [1]
      myFav & yrFav

  6. 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



  7. 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.





  8. Define or describe each of the following object-oriented terms: [8]
    1. Class: a user-defined container type

    2. Instance: an object; instances are to classes as variables are to types

    3. Attribute: a variable or method belonging to an object

    4. Method: a function/procedure belonging to an object

    5. Constructor/Initializer: the special method which is called when an object is instantiated

    6. Message: communication between objects; how methods are called

    7. Interface: a set of messages that an object can receive

    8. Overloading: giving multiple definitions to a function/method/operator depending on the type of its parameters/operands

  9. 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()
    












  10. 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!"
    














  11. (revised) Explain the differences among alias, shallow copy, and deep copy; use diagrams as appropriate. [3]