← Back to the Introduction to Computer Science outline The Course Maker
← Introduction to Computer Science outline
Week 15 · Quiz

Week 15 Quiz

Introduction to Computer Science Free sample edition
The student view of the 10-question quiz: each question and its options as they appear in Canvas. The answer key and the feedback ship only inside the course's .imscc (Classic QTI); they are not shown on this public page.
  1. Predict, then check: what does this program print?

    class Counter:
        def __init__(self):
            self.count = 0
    
        def increment(self):
            self.count = self.count + 1
    
    c1 = Counter()
    c2 = Counter()
    c1.increment()
    c1.increment()
    c2.increment()
    print(c1.count, c2.count)

    Select one.

    • 3 3
    • 2 1
    • 1 1
    • 2 2
  2. Predict, then check: what are the two lines this program prints?

    class ReadingLog:
        def __init__(self, goal):
            self.goal = goal
            self.pages_read = 0
    
        def add_day(self, pages):
            self.pages_read = self.pages_read + pages
    
        def pages_left(self):
            return self.goal - self.pages_read
    
    mine = ReadingLog(300)
    yours = ReadingLog(150)
    mine.add_day(45)
    mine.add_day(30)
    yours.add_day(45)
    print(mine.pages_read, mine.pages_left())
    print(yours.pages_read, yours.pages_left())

    Select one.

    • 75 225, then 120 30
    • 45 255, then 45 105
    • 75 225, then 45 105
    • 75 225, then 75 75
  3. This program never writes __init__ as a call. How many times does the line inside __init__ print, and why?

    class ReadingLog:
        def __init__(self, goal):
            print("building a log with goal", goal)
            self.goal = goal
            self.pages_read = 0
    
    mine = ReadingLog(300)
    yours = ReadingLog(150)
    print(mine.goal, yours.goal)

    Select one.

    • Twice, because each call to the class runs __init__ on the new object
    • Never, because a method runs only when its name is called
    • Once, when Python reads the class definition
    • Three times, once for each line that names mine or yours
  4. Which exception type ends this program?

    class ReadingLog:
        def __init__(self, goal):
            self.goal = goal
            self.pages_read = 0
    
        def add_day(self, pages):
            self.pages_read = self.pages_read + pages
    
        def pages_left(self):
            return self.goal - self.pages_read
    
    mine = ReadingLog(300)
    mine.add_day(mine, 45)
    print(mine.pages_read)

    Select one.

    • AttributeError
    • NameError
    • ValueError
    • TypeError
  5. Which exception type ends this program, the pinned missing-self example?

    class Cat:
        def __init__(self, name):
            name = name
    
        def speak(self):
            return self.name + " says Meow"
    
    c = Cat("Milo")
    print(c.speak())

    Select one.

    • TypeError
    • AttributeError
    • NameError
    • SyntaxError
  6. Predict, then check: what are the two lines this program prints?

    class ReadingLog:
        def __init__(self, goal):
            self.goal = goal
            self.pages_read = 0
    
        def add_day(self, pages):
            self.pages_read = self.pages_read + pages
    
        def pages_left(self):
            return self.goal - self.pages_read
    
        def report(self):
            print(self.pages_read, "of", self.goal)
    
    mine = ReadingLog(300)
    mine.add_day(45)
    result = mine.report()
    print(result)

    Select one.

    • 45 of 300, then None
    • 45 of 300, then 45 of 300
    • 45 of 300, then 255
    • None, then 45 of 300
  7. Which exception type ends this program?

    class ReadingLog:
        def __init__(self, goal):
            self.goal = goal
            self.pages_read = 0
    
        def add_day(self, pages):
            self.pages_read = self.pages_read + pages
    
        def pages_left(self):
            return self.goal - self.pages_read
    
    mine = ReadingLog(300)
    add_day(45)

    Select one.

    • TypeError
    • AttributeError
    • NameError
    • It adds 45 to mine and prints nothing
  8. Predict, then check: what does this program print?

    class ReadingLog:
        def __init__(self, goal):
            self.goal = goal
            self.pages_read = 0
    
        def add_day(self, pages):
            self.pages_read = self.pages_read + pages
    
        def pages_left(self):
            return self.goal - self.pages_read
    
    mine = ReadingLog(300)
    mine.add_day(45)
    same = mine
    same.add_day(30)
    print(mine.pages_read, same is mine)

    Select one.

    • 45 False
    • 30 False
    • 75 False
    • 75 True
  9. Predict, then check: what does this program print?

    class ReadingLog:
        def __init__(self, goal):
            self.goal = goal
            self.pages_read = 0
    
        def add_day(self, pages):
            pages_read = self.pages_read + pages
    
    mine = ReadingLog(300)
    mine.add_day(45)
    print(mine.pages_read)

    Select one.

    • 45
    • 0
    • AttributeError
    • None
  10. True or false: in the method header def add_day(self, pages):, the call mine.add_day(45) hands mine to self automatically, so the caller writes only the 45.

    True or false.

    • True
    • False

Built with The Course Maker · samples.thecoursemaker.com