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

Total points: 70
  1. An HSDPA cell-phone connection runs at 7.2Mb/s (mebibits per second). Assuming full utilization of the bandwidth (which never happens in reality), how long would it take to download a 0.9GB (gibibyte) file? [4]
    210 = 1024 sec = 17 min 4 sec

  2. Convert 95 (decimal) into binary, octal, and hexadecimal. Express the octal and hex results in syntax understandable to Python. [5]
    Binary: 1011111. Octal: 0137. Hex: 0x5f.

  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. What is a stub function, and why might such things be useful in software development?[5]
    In top-down design of a complex software system, it is helpful to build the general framework first, and fill in the details later. An analogy is building the frame of a house first, then filling in the wiring, plumbing, drywall, and appliances later. A stub function is a placeholder for a complete function; it does not perform the computations yet but always returns a dummy value, e.g., 0. An example is our ROT13 program, when we had the framework of converting between letters and ASCII codes, but did not actually do the ROT13 encryption arithmetic.





  5. Consider a Caesar substitution cipher on a reduced alphabet of just the letters A through L. Using the key 'KFHCLDAJEIBG', decode this ciphertext: K HKFFKAL DLC KFEAKEG [4]
    A CABBAGE FED ABIGAIL

  6. What are exceptions used for? What happens when one is raised? [4]
    Exceptions are used to signal an error or out-of-the-ordinary situation. When one is raised, normal execution of the program terminates, and control jumps to the nearest enclosing exception handler that can catch that exception. The exception handler typically would clean up and notify the user of the error, before either exiting gracefully or continuing on.





  7. Name and describe at least three standard exceptions that we learned. For each, write a short snippet of Python code or a situation that might raise that exception. [6]
    ZeroDivisionError: math division by zero: e.g., 5 / 0
    NameError: no such variable exists: e.g., myApples if no variable named myApples has been declared
    TypeError: type of operands does not fit the operator: e.g., 2 + "hello"
    SyntaxError: invalid Python syntax: e.g., in for: if
    KeyboardInterrupt: user pressed Control-C
    IOError: problem with opening a file: e.g., trying to read a file that doesn't exist





  8. What is recursion? How is it different from looping (e.g., for or while loops)? What conditions are needed to ensure that a recursive function will actually stop? [5]
    A recursive function calls itself; e.g., a function named "myFun()" may include a call to myFun() within its code. Recursion involves new function calls (thereby creating new stack frames), whereas looping can be done entirely within one function call (possibly saving memory space and improving speed). To prevent infinite recursion (where the function keeps calling itself indefinitely), progress must be made each time; e.g., if the parameter is n, the recursive call may be myFun(n-1). Also, a base case is needed as a stopping point.







  9. Define or describe each of the following terms in object-oriented programming: [6]
    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. Interface: a set of messages that an object can receive
  10. Tell me everything you know about pseudo-random number generators (PRNG). How are they different from "true" random number generators? Describe a situation in which a pseudo-random number generator might be preferable to a "true" random number generator. [5]
    A software RNG is a function that yields a new random number every time it is called. Usually, no parameters are required, and the function returns a float in the range [0,1). A software RNG works by taking the output of the last invocation of the function, performing a bunch of math to "randomize", and returning a new number in the appropriate range. As a result, it needs to keep track of the last generated value. The software RNG can be "seeded" with a user-supplied value; given the same seed, the RNG will always yield the same sequence of "random" numbers. An example application is a card game, where supplying the same seed results in exactly the same shuffle of the card deck; this can be useful if the user wants to replay the same game.







  11. (Do the remaining problems on separate paper.)
    Design a library for a pseudo-random number generator. For this question, no code is needed; just design the interface to the library: what functions should your library provide? Any global variables or constants? For each item, describe its role/purpose. For each function, give its pre/post-conditions (i.e., what would be in its docstring). You are designing the header file for the library. [5]
    Provide a function random() that takes no parameters, and produces a new pseudo-random number, say in the range [0,1).
    Provide a function seed(x) that takes one numeric parameter and initializes the seed for the PRNG. Nothing is returned. Setting the seed to the same number results in subsequent calls to random() producing the same sequence of numbers.
  12. Design a class Driver that represents any person in BC who has a driver's licence. What attributes are needed? What methods? No Python code is needed, just discussion. [5]
    Attributes: name, date of birth, address, photo, licence #, date licence was issued, date licence expires, insurance info, list of vehicles for which they are primary driver, class of licence (e.g., truck, bus, motorcycle, etc.), stage of graduated licence (L, N), ...
    Methods: renew licence, check if insurance is up-to-date, change address, upgrade class of licence (e.g., get motorcycle licence), ...
  13. Write a Python program to read in text from the file "input.txt", convert all text to uppercase, and write the result to the file "output.txt". (Hint: use string.upper()) Docstring, pseudocode, comments, etc. are not required, but may be helpful for partial credit if your code isn't right. [8]
    import string
    iFile = open("input.txt")
    oFile = open("output.txt", "w")
    for line in iFile:
    	oFile.write(string.upper(line))
    iFile.close()
    oFile.close()
    
    Or the more "proper" Python way:
    import string
    with open("input.txt") as iFile:
    	with open("output.txt", "w") as oFile:
    		for line in iFile:
    			oFile.write( string.upper(line) )
    
    Actually, Python strings have a built-in upper() method, so a solution may be as short as:
    with open("input.txt") as iFile:
    	with open("output.txt", "w") as oFile:
    		oFile.write( iFile.read().upper() )
    
  14. Modify your program from the previous question so it doesn't crash if the file "input.txt" does not exist, or if "output.txt" cannot be created. Make sure the file handles are always closed in any case. (If your solution to the previous problem already does this, just note that, and you will get full credit for this problem.) [4]
    Either use the more "proper" Python solution (using 'with'), or wrap all the code in a try/except block, catching IOError. Note that you should choose either to use 'with' or 'try/except', but not both.
    import string
    try:
    	ifile = open("input.txt")
    	ofile = open("output.txt", "w")
    	for line in ifile:
    		ofile.write(string.upper(line))
    except IOError:
    	print "Problem opening input.txt or output.txt!"
    finally:
    	ifile.close()
    	ofile.close()