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

Total points: 100
  1. Fill in the blank: [11]
    1. "Computers are tools__________, and computing scientists are toolsmiths________________."
    2. In software development, the document that describes what the client desires for the project is called the requirements________________.
    3. When a new instance (object) is created in Python, the method named __init__()_______________ is invoked.
    4. Testing of a single component in isolation (as opposed to integration testing) is called unit________ testing.
    5. What word describes functions which invoke themselves? recursion________
    6. The Python method which serializes an object and writes it out to a file is in the pickle____________ library and is called dump()______________.
    7. What English part of speech should an attribute name be? noun___________
    8. Let myList = [ 1, 3, 5, 7 ]. What is myList[2:2]? [] (empty list)_____________
    9. What method returns a list of all the indices in a dictionary? keys()________
  2. Compare and contrast Python lists with C/M2 arrays [4]
    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.
  3. Compare and contrast static typing versus dynamic typing. What are pros/cons of each? [4]
    (The key here is type: not whether the values of the variables can change, but whether their types can change.) Static typing: variables cannot change type Must declare type of variable and initialize value Compiler enforces correct type Dynamic typing: can change type More flexible, but usually we don't want variables to change type
  4. What is wrong with the following block of Python code? Indicate how you would fix the bug. [4]
    def fun(fu):
    	fa = fu * fun(fu - 1)
    	return fa
    print( fun(5) )
    
    Missing base case in recursive function; fun() will recurse forever!
  5. What is a stub function, and why might such things be useful in software development? Describe an example.[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.
  6. Consider the concepts of call-by-value and call-by-reference.
    1. Define and contrast these two terms as general concepts (i.e., independent of Python). [4] In call-by-value, the value in the actual parameter is copied to the formal parameter; the two are separate (so changes to the formal parameter will not affect the actual parameter).
      In call-by-reference, the formal parameter is a reference (alias) to the actual parameter, so changes in one show up in the other.
    2. Write Python code which clearly demonstrates the difference between the two. [4]
        	def doubler(x):
      		x *= 2
      	a = 7
      	doubler(a)
      	b = [ 3, 4, 5 ]
      	doubler(b)
      
      The first call to doubler() passes an int, so (in Python) it is passed by value. The function has no effect and does not return anything.
      The second call to doubler() passes a list, so (in Python) it is passed by reference. The function modifies the list in-place; it still does not return anything, but afterward the variable 'b' has the value '[3, 4, 5, 3, 4, 5]'.
  7. Evaluate each of the following Python expressions exactly as given. If it produces an error/exception, describe the error in words. Assume each expression is independent of the others. For all expressions, assume only the following initialization: [15]
    	myPear = "Red Bartlett"
    	from math import pi	
    1. 19492016 % 2 0
    2. [2, 4, 6] * 2 [2, 4, 6, 2, 4, 6]
    3. [2, 4, 6] + 8 TypeError: list + int
    4. 2 ** 3 > 5 or 3 / 0 True
    5. int("2 pears") ValueError
    6. "I have %06.2f pears" % 3.1415 'I have 003.14 pears'
    7. "03d%s" % (15, 'pears') TypeError (# args)
    8. 'b' in myPear False
    9. Bartlett.upper() NameError: Bartlett
    10. range(myPear) TypeError
    11. myPear[2:6] 'd Ba'
    12. chr(ord('P') + 3) 'S'
    13. 'pi' + 'e' 'pie'
    14. 'pie' - 'e' TypeError: str - str
    15. pi + 'e' TypeError: float + str

  8. 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. Overloading: giving multiple definitions to a function/method/operator depending on the type of its parameters/operands
  9. Name the five steps to top-down problem solving as we discussed in class. (It's okay if you can't remember the exact wording; the concepts are more important.) [3]

    Write, Apprehend, Design, Execute, Scrutinize
    ___________ ___________ ___________ ___________ ___________
  10. Discuss the limitations of the software development model used in the previous question, and describe at least two alternate models that we talked about in class that might better reflect real-world software development. Discuss pros and cons of the alternate models as compared with the model used in the previous question. [8]
    Waterfall (WADES) assumes perfect requirements, perfect specification, perfect design, etc. In reality, we never get that, and have to move on with imperfect documentation, often resulting in a poor product and an unhappy client. The "V" model is a slight modification of WADES that emphasizes testing of each stage (components, systems, releases, products) before completion. The spiral/agile model emphasizes repeated cycles of the waterfall model, early prototypes to get client feedback, and flexibility to changes in requirements, specification, or design.
  11. Consider a class Person that represents a single person in a social network like Facebook.
    1. What attributes are needed (indicate type of each attribute)? What methods are desirable? No Python code is needed, just discussion. [6]
    2. What other classes might you need to design a social network platform? What relationships might exist between those classes and the Person class? Discuss and sketch a class diagram. [6]
  12. Write a Python program which prints (on the screen) every word which appears exactly once (and no more than once) in the file "input.txt".

    Ignore case. Words are separated by spaces or newlines, and all punctuation, digits, etc. can be treated the same as letters. For instance, if the file "input.txt" contains:
    	Hello, this is a text file.  hello, here's a TEXT file.
    then your program should output "this here's is".
    (Order of the words in the output does not matter.)

    Ensure your program doesn't crash if the input file is not found, or if file permissions are incorrect, etc. Docstring, pseudocode, comments, etc. are not required, but may be helpful for partial credit if your code isn't right. [10]
    # Count each word in the file
    wordlist = {}
    with open("input.txt") as fileIn:
    	for line in fileIn:
    		for word in line.split():
    			lword = word.lower()
    			wordlist[lword] = wordlist.get(lword, 0) + 1
    # Print only the unique words
    for lword in wordlist:
    	if wordlist[lword] == 1:
    		print( lword, end=' ' )
    

  13. [Image courtesy Rochester Institute of Technology]
    One (very slow) way to calculate the value of π is to compare the area of the unit quarter-circle with the area of the unit square.

    We can estimate this by generating random points in the unit square (akin to throwing darts at the unit square), and counting how many lie within the quarter-circle. The ratio should approximate π/4 as the number of points increases. Some helpful hints:
    1. Your task is to design a function circle_pi() that uses the above algorithm to estimate π. The caller should be able to specify the number of points (darts) to use in the approximation. The function should be modular so that other code can easily use it -- it should not do any console I/O! Write a comprehensive docstring for this function (but don't implement it yet!), including pre- and post-conditions. [4]
    2. Write a comprehensive test suite for this function, including both valid and invalid input. Clearly indicate the invocation and the expected result. [4]
    3. Implement this function in Python. Pseudocode, comments, etc. are not required, but may be helpful for partial credit. You should not need to import math! [6]
    See circlepi.py