CSC 111 Quiz 5, Fall 2006

Submit by Tues November 21 by midnight


Name: First name :
Last name  :
Account name for copy (e.g., 111a-xx):
  
Quiz Code (password):

(Please take this quiz just once, unless you are told that the username or code is incorrect.
Write your quiz answers on paper before you hit the Submit Button,
in case you enter the wrong quiz password.)

1. Consider the class definition below:
class Vacation:
    def __init__(self, destination):
        self.destination = destination
    def setDate(self, date):
        details = string.split(date)
	self.month = details[0]
	self.day = details[1]
	self.year = details[2]
    def getYear():
        return self.year

def main():
    fantasy = Vacation("Cape Cod")
    fantasy.setDate("June 16 2007")
How do we get the year to print out in main()?
print Vacation.getYear()
print main.getYear()
print fantasy.getYear()
none of the above.
2. One of the benefits of object-oriented programming is: it makes math easier
it enables us to structure our data
it enables us to write more lines of code
None of the above.
3. Consider the python statements:
import time
class Face:
    def __init__(self, outline, eyeL, eyeR, nose, mouth):
        self.eyeL = eyeL
	self.eyeR = eyeR
	self.nose = nose
	self.mouth = mouth
	self.outline = outline
	self.background = 'cyan'
	self.foreground = 'black'
    def wink(self, length):
        self.eyeL.setFill(self.background)
	time.sleep(length)
	self.eyeL.setFill(self.foreground)
def makeFace(win):
    c = Circle(Point(100,100),80)
    c.draw(win)
    e1 = Circle(Point(30,60),15)
    e1.draw(win)
    e2 = e1.clone()
    e2.move(40,0)
    e2.draw(win)
    n = Circle(Point(50,40),10)
    n.draw(win)
    m = Rectangle(Point(30,15),Point(70,25))
    m.draw(win)
    f = Face(c,e1,e2,n,m)
In function makeFace(), how do I get the left eye to:
wink 3 times for 2 seconds per wink then
wink 3 times for 1 second per wink?

    for i in range(3):
        f.wink(2)
        f.wink(1)

    for i in range(2):
        f.wink(3)
    for i in range(1):
        f.wink(3)

    for i in range(3):
        f.wink(2)
    for i in range(3):
        f.wink(1)
none of the above
4. Consider the function definition:
import math 

def safeSqrt(num):
    if num < 0:
        x = math.sqrt(num)
    else:
        x = "Error"
    return x

def main():
    mynum = -25
    print safeSqrt(mynum)
What happens when we run main()?
-5 is printed
5 is printed
Just the string "Error" is printed
none of the above
5. True or False:
A method definitions's formal parameter self
can appear anywhere in the formal parameters listed
between the parentheses in the method definition.
True
False
6. True or False: A programmer cannot define classes. True
False
7. Consider the following python code:
class CD:
    def __init__(self, artist, title):
        self.artist = artist
	self.title = title
    def getTitle():
        return self.title

def main():
    infile = open("music.txt", "r")
    cds = infile.readlines()
    cdObjects = []
    for i in cds:
        one = string.split(i)
	nextCD = CD(one[0], one[1]))
	cdObjects.append(nextCD)
At the end of the definition of main(), how do I obtain
the title of the first cd stored in music.txt?
cdObjects[0].getTitle()
cd[0].title
nextCD[0].getTitle()
8. In the first part of chapter 9, the top-down design
of a complex program is explained. An outline of the program
was written, and the definition of the main() function was written.

True or False:
The details of how the functions were defined had to be worked out
before the function calls were inserted in the definition of main().
True
False
9. Suppose I have the following while loop:
win = GraphWin("Mouse Proof", 200,200)
win.setCoords(0,0,200,200)
pt = win.getMouse()
while pt.getX() > 5000:
    pt = win.getMouse()
    print pt.getX()
What happens when this executes
and the user keeps clicking the mouse?
no values are printed
one value is printed
an infinite number of values are printed
10. In Chapter 9, once the idea of top-down design is
described, the concept of Bottom-Up Implementation is described.

True or False: In Bottom-Up Implementation one types in the whole
program at once and sees if it works.
True
False

To submit:

Press to submit your fully-completed quiz, or to reset all fields. Thanks!