CSC 108H1 S 2012 Test 1 Duration - 45 Minutes Aids Allowed: None
CSC 108H1 S 2012 Test 1 Duration - 45 Minutes Aids Allowed: None
Duration 45 minutes
Aids allowed: none
Student Number:
Last Name: First Name:
Lecture Section: L0101 Instructor: Campbell
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 and docstrings 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. No error checking is required:
assume all user input and all argument values are valid.
If you use any space for rough work, indicate clearly what you want marked.
# 1: / 2
# 2: / 2
# 3: / 8
# 4: / 8
TOTAL: /20
Total Pages = 6 Page 1 contd. . .
CSC108H1 S Test 1 Winter 2012
Question 1. [2 marks]
Part (a) [1 mark] What is the output of the following?
pic = media.create_picture(50, 100)
pic2 = media.add_text(pic, 0, 0, test, media.yellow)
print type(pic2)
Part (b) [1 mark] Rewrite the following code without an if-statement.
if ketchup and not mustard:
return True
else:
return False
Question 2. [2 marks]
In each question below, ll in the box with python code that will make the program behaviour match the
comments. You may not make any other changes to the code.
Part (a) [1 mark]
name = Matthew
age = 3
# Print the following: Matthew is 3!
print % (name, age)
Part (b) [1 mark]
pic = media.load_picture(media.choose_file())
# get the pixel at (10, 4)
# set the pixel at (10, 4) to yellow
media.set_color(pix, media.yellow)
Student #: Page 2 of 6 contd. . .
CSC108H1 S Test 1 Winter 2012
Question 3. [8 marks]
Part (a) [4 marks] Complete the following function according to its docstring description.
def change_green(pic, factor):
(Picture, float) -> Picture
Return a new picture that is a copy of pic, but with each pixels green color
component set to its original value multiplied by factor. factor is a value
between 0.0 and 1.0, inclusive.
Part (b) [4 marks]
Write a main block that allows the user to choose a le, prompts the user with, Enter a value between
0.0 and 1.0, inclusive: , applies the change green function from part (a) to the picture in that
le using the value entered by the user, and displays the resulting picture. You may assume that the user
chooses a valid picture le and enters a valid value.
if __name__ == __main__:
Student #: Page 3 of 6 contd. . .
CSC108H1 S Test 1 Winter 2012
Question 4. [8 marks]
Consider the following two .py les, which are saved in the same directory (folder).
module_a.py: module_b.py:
def f(s): import module_a
result =
def g(s):
for char in s: answer = module_a.f(s)
if char == char.upper(): return len(answer)
result = result + char
if __name__ == __main__:
return result print module_a.f(WXyZ)
print g(TeSTiNg)
if __name__ == __main__:
print f(EFg)
# this code is not inside the
# body of the if-statement
print f(aBcde)
This question continues on the next page. You may use the space below for rough work.
Student #: Page 4 of 6 contd. . .
CSC108H1 S Test 1 Winter 2012
Part (a) [1 mark]
How many lines of output are produced when module b is executed (by clicking Run)?
Circle one: 2 lines 3 lines 4 lines
Part (b) [4 marks]
In the table below, show the output from running module b. If there are fewer than four lines of output,
leave the unused box(es) empty.
Part (c) [3 marks]
Write a good docstring for the function f from module a.
Student #: Page 5 of 6 contd. . .
Last Name: First Name:
Short Python function/method descriptions:
__builtins__:
len(x) -> int
Return the length of the list, tuple, dict, or string x.
raw_input([prompt]) -> str
Read a string from standard input. The trailing newline is stripped.
float:
float(x) -> float
Convert a string or number to a floating point number, if possible.
int:
int(x) -> int
Convert a string or number to an integer, if possible. A floating point
argument will be truncated towards zero.
media:
add_text(pic, x, y, s, col)
Draw the str s in Color col on Picture pic starting at (x, y).
choose_file() --> str
Prompt user to pick a file. Return the path to that file.
copy(Picture) -> Picture
Return a copy of the Picture.
create_picture(int, int) --> Picture
Given a width and a height, return a Picture with that width and height. All pixels are white.
get_blue(Pixel) --> int
Return the blue value of the given Pixel.
get_color(Pixel) --> Color
Return the Color object with the given Pixels RGB values.
get_green(Pixel) --> int
Return the green value of the given Pixel.
get_pixel(Picture, int, int) --> Pixel
Given x and y coordinates, return the Pixel at (x, y) in the given Picture.
get_red(Pixel) --> int
Return the red value of the given Pixel.
load_picture(str) --> Picture
Return a Picture object from file with the given filename.
set_blue(Pixel, int)
Set the blue value of the given Pixel to the given int value.
set_color(Pixel, Color)
Set the RGB values of the given Pixel to those of the given Color.
set_green(Pixel, int)
Set the green value of the given Pixel to the given int value.
set_red(Pixel, int)
Set the red value of the given Pixel to the given int value.
show(Picture)
Display the given Picture.
Colors: black: RGB: 0, 0, 0 white: RGB: 255, 255, 255 yellow: RGB: 255, 255, 0
str:
str(x) -> str
Convert an object into its string representation, if possible.
S.upper() -> string
Return a copy of the string S converted to uppercase.
Total Pages = 6 Page 6 End of Examination