Name: _________K E Y
Student ID: __________________

[total: 80pts]
  1. Describe the five program structure/flow
    abstractions mentioned in the text. [5]

    _Sequence__________ _Selection__________ _Repetition__________ _Composition__________ _Parallelism__________
  2. Describe the five hardware abstractions mentioned in the text. [5]

    _Input__________ _Storage__________ _Processing__________ _Control__________ _Output__________
  3. Fill in the blank: [16]
    1. "Computers are tools, and computing scientists are toolsmiths."
    2. A procedure that invokes itself is called a recursive procedure.
    3. In statically-typed languages, before we can use the value of a variable for the first time in
      a program, we need to declare and initialize it.
    4. True: The word define is a legal Python identifier.
    5. What English part of speech should a variable generally be? Noun
    6. Assume myFile is a file handle that has already been opened for reading. We wish to ensure that the next read() starts from the beginning of the file. What command do we use? seek(0)
    7. The standard function that converts strings to ALL-CAPS is found in the
      string standard library and is named upper().
    8. The standard function that evaluates natural logarithms is found in the
      math standard library and is named log().
    9. The standard error I/O channel is found in the
      sys standard library and is named stderr.
    10. A requirement imposed by a function upon its parameters, such that the correctness of the result is not guaranteed if the requirement is not met, is called a precondition / predicate.
    11. A RAM module contains 32 chips, each of size 256Mb. What is the total capacity of the RAM module, in bytes/kilobytes/megabytes/gigabytes (use binary units)? 1GB.
  4. Name two operations/functions/properties that Python lists have that M2/C arrays do not. [2]
    concatenation, repetition, slice, insert/delete
  5. Explain all the differences between the following, and give examples of each: [6]
    1. reserved words:
      Syntactical punctuation, part of the language. e.g., def, if, while
    2. standard identifiers: Names builtin to the language, e.g. names of builtin types. e.g., bool, float, int.
    3. standard library items: Names of procedures/types/variables included in the standard library modules that come with every Python installation. e.g., math.pi, sys.stderr
  6. What is a backtrace? [3]
    Shows the call stack: list of what functions are currently being evaluated
  7. Assume that the positive integer with binary representation 1100101 is stored in variable x. Evaluate as Python: [4]
    1. hex(x) 0x65
    2. oct(x) 0145
  8. Use the Python range() function to create this list: [ 11, 8, 5, 2 ]. [3]
  9. Write Python code to create a 15x25 matrix, using lists. [4]
    matrix = [0] * 25
    for row in range(len(matrix)):
    	matrix[row] = [0] * 15
  10. What is wrong with the following block of Python code? How would you fix it? [4]
    apples = [ 'Fuji', 'Gala', 'Braeburn', 'Rome' ]
    idx = 1
    while idx <= len(apples):
    	print apples[idx],
    	idx += 1
    The index iterates over the wrong range. Python lists are indexed starting at zero. As-is, the last time through the loop will result in an error, accessing apples[4]. Try initalizing idx to 0, then making the while loop condition be idx < len(apples).
  11. Evaluate each of the following Python expressions exactly as given, or if it produces an error, describe the error. Assume each expression is independent of the others. Assume all necessary imports have been done. For all expressions, assume the following initialization:
    	myApple = "Braeburn"	
    1. 2 + 3>4 and (5/0 < 1) ZeroDivisionError
    2. 2.0 + 14 / 4 5.0
    3. "%03d%s" % (27.95, 'apples') '027apples'
    4. "%05.2f" % 3.1415926 '03.14'
    5. len(myApple) = 7 SyntaxError: can't assign
    6. range(len(myApple)) [0, 1, 2, 3, 4, 5, 6, 7]
    7. myApple[2:5] 'aeb'
    8. string.upper(myApple[-1]) 'N'
    9. myApple < "Brayburn" True
    10. 'z' < 'w' False
    11. ord('z') - ord('w') 3
    12. 'z' - 'w' TypeError: can't subtract string
    13. 'z' + 'w' 'zw'
    14. 6 & 5 4

  12. What is output by the following block of Python code? [4]
    def increment(x, y):
    	x += y
    a = 3
    b = range(3)
    increment(a, 1)
    increment(b, [1])
    print "a = ", a, "b = ", b

    'a = 3 b = [0, 1, 2, 1]'
  13. What is output by the following block of Python code? [3]
    myList = [ ('a', 'b'), range(5), [ [2, 3, 4], 5], 'xyz' ]
    for item in myList:
    	print len(item),

    '2 5 2 3'
  14. Assume that myList is a Python list. Explain all the differences between
    yrList = myList and yrList = myList[:]. [3]
    The basic difference is between aliasing (the first command) and copying (the second command). If the first command is used, then any changes to yrList will be reflected in myList, and vice versa, because both names refer to the same list. In the second case, yrList is a copy of myList, so changes to one will not affect the other.
  15. Tell me everything you know about libraries in the context of programming. [4]
    Libraries are containers/groups of functions and other entities provided for other programmers to use. A well-designed library will have a clear public interface (header or definition file) which tells the programmer what functionality the library provides and how to invoke its functions. The other component of the library is its implmentation, which is the code or bodies of its functions. The implmentation will often be hidden from the user. Some examples of standard Python libraries include math, sys, string, random. An example of a function provided by a library is math.sin(). An example of a non-function entity provided by a library is math.pi; it is a constant rather than a function.