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

Total points: 57
  1. Define: [4]
    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. Megabyte: either 220 bytes (binary units) or 106 bytes (SI units)
  2. Convert 315 (decimal) to binary, octal, and hexadecimal. [6]

    100111011, 0473, 0x13b



  3. Describe the cylinder/head/sector geometry of hard drives. [4]

    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.






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







  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, Rome, and Melba.
    1. Find the intersection of myFav and yrFav. Express in words the interpretation of this in the context of apple preferences. [2]
      { Fuji, Rome }. Those varieties that both you and I like.


    2. Find the set difference yrFav - myFav. Express in words the interpretation of this in the context of apple preferences. [2]
      { Melba }. Those varieties that you like but I don't like


    3. 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. [2]
      fuji = 1 << 0
      gala = 1 << 1
      spartan = 1 << 2
      rome = 1 << 3
      melba = 1 << 4
      
    4. Using the variables you just defined, define a Python bitset for yrFav. [2]
      yrFav = fuji | rome | melba
      
    5. Write a Python expression for the intersection of myFav and yrFav. [2]
      myFav & yrFav


  6. Define or describe each of the following object-oriented terms: [8]
    1. Message: communication between objects; how methods are called
    2. Interface: a set of messages that an object can receive
    3. Class: a user-defined container type
    4. Instance: an object; instances are to classes as variables are to types
    5. Attribute: a variable or method belonging to an object
    6. Method: a function/procedure belonging to an object
    7. Constructor/Initializer: the special method which is called when an object is instantiated
    8. Sub-class: a class which inherits attributes/methods from a parent class
  7. Given the following block of Python code, consider the names visible to the print statement at the end. For each of the expressions in the print statement, write what would be printed, or if the name is not visible, write 'NameError'. [8]
    a = 1
    def f(p):
    	global a
    	from math import sqrt
    	a = p
    class C:
    	a = 2
    	b = 3
    	def __init__(self, d=4):
    		f(5)
    		a = 6
    		b = 7
    		self.e = 8
    g = C()
    print a, b, d, e, g.a, g.b, g.d, g.e
    a=5, b=NameError, d=NameError, e=NameError, g.a=2, g.b=3, g.d=AttributeError, g.e=8
    
  8. Using exceptions, modify the following block of Python code to print, "Can't divide by zero; goodbye!" and exit the loop when the user inputs 0. [4]
    
    try:
    
    	while True:
    	
    		in = input("Type a number and I'll invert it! ")
    
    		recip = 1.0/in
    
    		print "The reciprocal is:", recip
    
    except ZeroDivisionError:
    	print "Can't divide by zero; goodbye!"
    
    	
    
  9. An example application of dictionaries is to implement "sparse matrices" -- where only the non-zero entries of a matrix are stored. The keys are (row,col) tuples, and the values are the corresponding non-zero entries of the matrix. For example, a 2x2 sparse matrix with the value 5 in entry (1,0) and the value 3 in entry (1,1) would be represented by the dictionary {(1,0): 5, (1,1): 3}.
    1. Write a Python function that returns the number of non-zero entries in a sparse matrix. (Hint: it should be a very short function!) Include a short docstring. [2]
      def count_nonzero(matrix):
      	"""Count the number of non-zero entries in a sparse matrix.
      	Assumes that only non-zero entries are represented in the
      	dictionary."""
      	return len(matrix)
      
    2. Write a Python function that returns the number of non-zero entries in a given row of a sparse matrix. You may assume the caller will pass in two additional parameters, nrows and ncols, specifying the number of rows and columns in the matrix. Include a short docstring. [4]











  10. Come up with your own application for dictionaries (different from the previous problem). What do the keys represent? What do the values represent? Create a sample dictionary to illustrate your application. [3]