In-Class Questions and Video Solutions

Lecture 9

  1. Getters and Setters

    Which of the below is a getter method for the number of wheels?

    ----------- Given ------------
    class Car(object):
     def __init__(self, w, d):
     self.wheels = w
     self.doors = d
     self.color = ""
    (A)    def get_wheels():
     return wheels
    (B)    def get_wheels():
     return self.wheels
    (C)    def get_wheels(self):
     return wheels
    (D)    def get_wheels(self):
     return self.wheels

    def get_wheels(): close
    def get_wheels(): close
    def get_wheels(self): close
    def get_wheels(self): check
    Check Show Solution

  2. Subclass

    Whate line could replace ____blank____ to create a class that inherets from Animal in the code below?

     def speak(self):
      print("ruff ruff")
    (line1) d = Dog(7)
    (line2) d.set_name("Ruffles")
    (line3) d.speak()

     class Dog(Animal):  check
     class Animal(Dog):  close
     class Dog(object)  close

With this definition of Dog, you run a program with line1, line2, and line3 above. What happens? Refer to the lecture slides for the code making up the “Animal” class.

 Error on (line1) because Dog does not have an \_\_init\_\_  close
 Uses the \_\_init\_\_ from Animal, but then an error (line2) because Dog does not have a set\_name method.  close
 Uses the \_\_init\_\_ and set\_name from Animal, but then an error (line3) because all methods must return something.  close
 Runs, creates a Dog object with age=7 and name="Ruffles", and prints "ruff ruff"  check
Check Show Solution

Course Info
Learning Resource Types
assignment Problem Sets
notes Lecture Notes
theaters Lecture Videos
assignment_turned_in Programming Assignments with Examples