You are on page 1of 6

CSC108H5 F 2010 Test 2

Duration 45 minutes
Aids allowed: none
Student Number:
Lab time or TA name:
Last Name: First Name:
Instructor: Craig
Do not turn this page until you have received the signal to start.
(Please ll out the identication section above, write your name on the back
of the test, and read the instructions below.)
Good Luck!
This midterm consists of 4 questions on 6 pages (including this one). When
you receive the signal to start, please make sure that your copy is complete.
Comments are not required except where indicated, although they may help
us mark your answers. They may also get you part marks if you cant gure
out how to write the code.
If you use any space for rough work, indicate clearly what you want marked.
# 1: / 4
# 2: / 4
# 3: / 8
# 4: / 4
TOTAL: /20
Total Pages = 6 Page 1 contd. . .
CSC108H5 F Test 2 Fall 2010
Question 1. [4 marks]
Write a python program that repeatedly asks the user to enter a number and stops only when the user
enters 999. Once it stops asking for input, the program prints the total of all the positive numbers that
were entered. It does not include the negative numbers in the total but just ignores them. The nal 999
is not included in the total. For example, if the user entered the following numbers in order: 4, -9, 10.6,
21, -100, 999, the program would print The total of the positive numbers is 35.6 Your program
may assume that the user always enters a number, so it does not need to work if the user enters a value
such as zero or some string.
Page 2 of 6 contd. . .
CSC108H5 F Test 2 Fall 2010
Question 2. [4 marks]
The following program does not do what the programmer is expecting and the programmer is using the
debugger to try to nd the problem. The programmer has added a breakpoint at line 9 and pressed the
key necessary to run the program up to this point. This particular debugger highlights the line that would
be next to execute. Line 9 is highlighted.
1. def get_slice_of_bread(bread):
2. bread = slice of + bread + bread\n
3. return bread
4. def mix_up_filling(filling):
5. filling.upper()
6. return filling + \n
7. def make_sandwich(filling, bread):
8. result = get_slice_of_bread(bread)
9. result += mix_up_filling(filling)
10. result += get_slice_of_bread(bread)
11. return result
12. if __name__ == __main__:
13. expected_result = "slice of brown bread\nTUNA\nslice of brown bread\n"
14. if make_sandwich(tuna , brown) != expected_result:
15. print oops
Part (a) [1 mark]
If the programmer chooses step over, what line will be highlighted next? (I.e. when the step over operation
is nished.)
Line Number:
Part (b) [1 mark]
If the programmer chooses step into, what line will be highlighted next? (I.e. when the step into operation
is nished.)
Line Number:
Part (c) [1 mark]
If the programmer chooses step out, what line will be highlighted next? (I.e. when the step out operation
is nished.)
Line Number:
Part (d) [1 mark] Fix the program by writing your changes directly on the code above. Print neatly.
Page 3 of 6 contd. . .
CSC108H5 F Test 2 Fall 2010
Question 3. [8 marks]
Part (a) [4 marks]
Complete function cut string to match its docstring. Note that delimiter may be longer than one
character. You may not use str.split() but str.find() is allowed. Here is the abbreviated result of
calling help(str.find)
find(...)
S.find(sub) -> int
Return the lowest index in S where substring sub is found.
Return -1 on failure.
def cut_string(original, delimiter):
Return a 2-element tuple consisting of the substrings of string original
that occur before and after the first occurence of string delimiter. If delimiter
does not occur in original, the first element of the returned tuple should be the
original string and the second element should be an empty string.
Page 4 of 6 contd. . .
CSC108H5 F Test 2 Fall 2010
Part (b) [4 marks] In the table below give 4 dierent test cases for cut string that would be a
reasonable set if you were limited to only 5 tests. Do not test invalid input. In other words, in your test
cases, original and delimiter must both be strings.
original delimiter expected return value test-case description
xxxDELyyyyy DEL (xxx,yyyyy) one occurence in middle
Question 4. [4 marks]
Complete the function build string so that it behaves according to its docstring. You may not use the
join method in the string class.
def build_string(L, connector):
L is a list of string elements. Return a single string that is the concatenation
of all elements in L each separated by string connector. L is unchanged.
Page 5 of 6 contd. . .
Last Name: First Name:
__builtins__:
len(x) -> integer
Return the length of the list, tuple, dict, or string x.
open(name[, mode]) -> file object
Open a file. Legal modes are "r" (read), "w" (write), and "a" (append).
range([start], stop, [step]) -> list of integers
Return a list containing the integers starting with start and ending with
stop - 1 with step specifying the amount to increment (or decrement).
If start is not specified, the list starts at 0. If step is not specified,
the values are incremented by 1.
float:
float(x) -> floating point number
Convert a string or number to a floating point number, if possible.
int:
int(x) -> integer
Convert a string or number to an integer, if possible. A floating point
argument will be truncated towards zero.
list:
x in L --> boolean
Return True if x is in L and False otherwise.
L.append(x)
Append x to the end of the list L.
L.index(value) -> integer
Returns the lowest index of value in L.
L.insert(index, x)
Insert x at position index.
L.remove(value)
Removes the first occurrence of value from L.
L.reverse()
Reverse *IN PLACE*
L.sort()
Sorts the list in ascending order.
str:
x in s --> boolean
Return True if x is in s and False otherwise.
str(x) -> string
Convert an object into its string representation, if possible.
S.find(sub[,i]) -> integer
Return the lowest index in S (starting at S[i], if i is given) where the
string sub is found or -1 if sub does not occur in S.
S.index(sub) -> integer
Like find but raises an exception if sub does not occur in S.
S.lower() / S.upper() -> string
Return a copy of the string S converted to lowercase/uppercase.
S.replace(old, new) -> string
Return a copy of string S with all occurrences of the string old replaced
with the string new.
S.split([sep]) -> list of strings
Return a list of the words in S, using string sep as the separator and
any whitespace string if sep is not specified.
S.strip() -> string
Return a copy of S with leading and trailing whitespace removed.
Total Pages = 6 Page 6 End of Examination