-
Describe the five program structure/flow
abstractions mentioned in the text. [5]
_Sequence__________ |
_Selection__________ |
_Repetition__________ |
_Composition__________ |
_Parallelism__________ |
-
Describe the five hardware
abstractions mentioned in the text. [5]
_Input__________ |
_Storage__________ |
_Processing__________ |
_Control__________ |
_Output__________ |
- Fill in the blank: [16]
- "Computers are tools, and computing scientists are toolsmiths."
- A procedure that invokes itself is called a recursive procedure.
- 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.
- True: The word define
is a legal Python identifier.
- What English part of speech should a variable generally be? Noun
- 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)
- The standard function that converts strings to ALL-CAPS is found in the
string standard library and is named upper().
- The standard function that evaluates natural logarithms is found in the
math standard library and is named log().
- The standard error I/O channel is found in the
sys standard library and is named stderr.
- 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.
- 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.
-
Name two operations/functions/properties that Python lists have that
M2/C arrays do not. [2]
concatenation, repetition, slice, insert/delete
-
Explain all the differences between the following, and give examples of each:
[6]
- reserved words:
Syntactical punctuation, part of the language. e.g., def, if, while
- standard identifiers:
Names builtin to the language, e.g. names of builtin types.
e.g., bool, float, int.
- 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
-
What is a backtrace? [3]
Shows the call stack: list of what functions are currently being evaluated
-
Assume that the positive integer with binary representation
1100101 is stored in variable x. Evaluate as Python:
[4]
- hex(x) 0x65
- oct(x) 0145
-
Use the Python range() function to create this list:
[ 11, 8, 5, 2 ]. [3]
range(11,-1,-3)
-
Write Python code to create a 15x25 matrix, using lists. [4]
matrix = [0] * 25
for row in range(len(matrix)):
matrix[row] = [0] * 15
-
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).
-
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"
[14]
- 2 + 3>4 and (5/0 < 1) ZeroDivisionError
- 2.0 + 14 / 4 5.0
- "%03d%s" % (27.95, 'apples') '027apples'
- "%05.2f" % 3.1415926 '03.14'
- len(myApple) = 7 SyntaxError: can't assign
- range(len(myApple)) [0, 1, 2, 3, 4, 5, 6, 7]
- myApple[2:5] 'aeb'
- string.upper(myApple[-1]) 'N'
- myApple < "Brayburn" True
- 'z' < 'w' False
- ord('z') - ord('w') 3
- 'z' - 'w' TypeError: can't subtract string
- 'z' + 'w' 'zw'
- 6 & 5 4
-
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]'
-
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'
-
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.
-
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.