Classes and Objects Quiz Study Guide
What is the output of this program? class Toothpaste: flavor = "Mint" # size -- the number of ounces in a tube def __init__(self, size=6.3): self.size = size def __repr__(self): return "A " + str(self.size) + " tube of " + self.flavor + " toothpaste" class ChildToothpaste(Toothpaste): flavor = "Bubble gum" # Main Program whitening = Toothpaste() clean = ChildToothpaste() print whitening print clean
A 6.3 tube of Mint toothpaste. A 6.3 tube of Bubble gum toothpaste.
Given the following code, which of the statements below is true about the relationship between these classes? class WaterBottle: # capacity -- the number of ounces the water bottle holds def __init__(self, capacity=24): self.capacity = capacity self.fullness = capacity # Take a drink # A drink takes 1 ounce away from the amount of liquid # actually held in the bottle def take_drink(self): if self.fullness > 0: self.fullness -= 1 def refill(self, num_ounces_added): if self.fullness + num_ounces_added <= self.capacity: self.fullness += num_ounces_added class AutomaticWaterBottle(WaterBottle): # temp -- the temperature at which to keep the liquid inside def __init__(self, capacity, temp): WaterBottle.__init__(self, capacity) self.temp = temp # more methods
AutomaticWaterBottle inherits from WaterBottle
What data type is returned from the __eq__ function?
Boolean
What is the output of the following program? class NationalPark: nation = "Chile" def __init__(self, name, area, location, year_established): self.name = name self.area = area self.location = location self.established = year_established def __repr__(self): return self.name + " National Park in" + self.location def print_complete_info(self): print self.name + " National Park, Est." + str(self.established) print "Location: " + self.location print "Area: " + str(self.area) # Main program chaco = NationalPark("Chaco", 58, "Chaco Province", 1954) print chaco.nation print NationalPark.nation chaco.nation = "Argentina" print chaco.nation print NationalPark.nation
Chile Chile Argentina Chile
Here is a partial definition of a User class. class User: user_id = 0 def __init__(self, name, age = 0): self.name = name self.age = age self.user_id = User.user_id User.user_id = User.user_id + 1 def __repr__(self): print self.age return "Name: " + self.name + ", ID: " + str(self.user_id) What kind of variable is user_id?
Class variable
Which of the following inheritance chains is not a good use of inheritance?
Cup inherits from Cat
Consider this partial class definition for a Cellphone class. Which of the following statements is true about Cellphone when an instance is first created?
Every instance is guaranteed to have the attributes color, model, and weight
What does the __init__ method do in a class definition?
It is a method called when a new instance of a class is created. It sets up instance variables and performs any other set up that needs to be done for the class.
What does the __eq__ method do in a class definition?
It is a method called when the == operator is called on an instance. It checks if two objects are equal.
What does the __repr__ method do in a class definition?
It is a method called when the instance needs to be printed. It provides a useful, human-readable description of an object.
Which of the lines labelled below would print "Bubble gum"? class Toothpaste: flavor = "Mint" # size -- the number of ounces in a tube def __init__(self, size=6.3): self.size = size def __repr__(self): return "A " + str(self.size) + " tube of " + self.flavor + " toothpaste" class ChildToothpaste(Toothpaste): flavor = "Bubble gum" # Main program whitening = Toothpaste() clean = ChildToothpaste() print clean.flavor # Line 1 print whitening.flavor # Line 2 print Toothpaste.flavor # Line 3 print ChildToothpaste.flavor # Line 4
Line 1 and Line 4
What does self refer to in a method?
The instance calling the method
What is the purpose of using inheritance among classes?
To easily add additional functionality to an existing class. It makes your code easier to maintain, since code is not copied all over the place.
What is the purpose of using classes?
To logically group data and methods together.
Which of the following is not a valid way to import and use the tan function from the math module?
import math.tan print tan(180)
Which of the following snippets correctly defines the method get_value for the Gemstone class shown below?
# returns how much the stone is worth def get_value(self): return self.weight * self.cost_per_oz
Here is a partial definition of a User class. class User: user_id = 0 def __init__(self, name, age = 0): self.name = name self.age = age self.user_id = User.user_id User.user_id = User.user_id + 1 def print_id(self): print self.age return "Name: " + self.name + ", ID: " + str(self.user_id) If an object were created as shown below, what would the print statement in print_id output if called on the object?
15
Here is a partial definition of a User class. class User: user_id = 0 def __init__(self, name, age = 0): self.name = name self.age = age self.user_id = User.user_id User.user_id = User.user_id + 1 def __repr__(self): print self.age return "Name: " + self.name + ", ID: " + str(self.user_id) Given the following object declarations, what would the value of tomiko.user_id be? roxie = User("Roxie", 14) lyle = User("Lyle", 8) tomiko = User("Tomiko", 24)
2
Given the following piece of code, what will be printed when the loop ends? class Animal: # Animal class implementation class Cat(Animal): # Cat class implementation # Main program pets = [Cat(), Animal(), Animal(), Cat(), Cat()] num_cats = 0 for elem in pets: if isinstance(elem, Cat): num_cats += 1 print num_cats
3
Given the following piece of code, what will be printed when the loop ends? class Animal: # Animal class implementation class Cat(Animal): # Cat class implementation # Main program pets = [Cat(), Animal(), Animal(), Cat(), Cat()] num_cats = 0 for elem in pets: if isinstance(elem, Animal): num_cats += 1 print num_cats
5