Quiz 10: How to define and use your own classes

¡Supera tus tareas y exámenes ahora con Quizwiz!

Consider the following code: class Rectangle: def __init__(self, height, width): self.height = height self.width = width def getPerimeter(self): perimeter = self.height * 2 + self.width * 2 return perimeter def getArea(self): area = self.height * self.width return area def getStr(self): return str(self.height) + " by " + str(self.width) How many public attributes does this class define?

2

Consider the following code: class Rectangle: def __init__(self, height, width): self.height = height self.width = width def getPerimeter(self): perimeter = self.height * 2 + self.width * 2 return perimeter def getArea(self): area = self.height * self.width return area def getStr(self): return str(self.height) + " by " + str(self.width) How many public methods does this class define?

3

Consider the following code: class Multiplier: def __init__(self): self.num1 = 0 self.num2 = 0 def getProduct(self): return self.num1 * self.num2 def main(): m = Multiplier() m.num1 = 7 m.num2 = 3 print(m.num1, "X", m.num2, "=", m.getProduct()) if __name__ == "__main__": main() When this code is executed, what does it print to the console?

7 X 3 = 21

Consider the following code: class Rectangle: def __init__(self, height, width): self.height = height self.width = width def getPerimeter(self): perimeter = self.height * 2 + self.width * 2 return perimeter def getArea(self): area = self.height * self.width return area def getStr(self): return str(self.height) + " by " + str(self.width) Which of the following adds an attribute named color to the class?

def __init__(self, height, width): self.height = height self.width = width self.color = "red"

Consider the following code: class Multiplier: def __init__(self): self.num1 = 0 self.num2 = 0 def getProduct(self): return self.num1 * self.num2 def main(): m = Multiplier() m.num1 = 7 m.num2 = 3 print(m.num1, "X", m.num2, "=", m.getProduct()) if __name__ == "__main__": main() To allow the constructor to optionally set values for its attributes, you could code it like this:

def __init__(self, num1, num2): self.num1 = 0 self.num2 = 0

Consider the following code: class Rectangle: def __init__(self, height, width): self.height = height self.width = width def getPerimeter(self): perimeter = self.height * 2 + self.width * 2 return perimeter def getArea(self): area = self.height * self.width return area def getStr(self): return str(self.height) + " by " + str(self.width) What is another way to code the getArea() method and have it perform the same task?

def getArea(self): return self.height * self.width

Consider the following code: class Rectangle: def __init__(self, height, width): self.height = height self.width = width def getPerimeter(self): perimeter = self.height * 2 + self.width * 2 return perimeter def getArea(self): area = self.height * self.width return area def getStr(self): return str(self.height) + " by " + str(self.width) Which of the following is the code for the constructor?

def getStr(self): return str(self.height) + " by " + str(self.width)

A UML class diagram

describes the attributes and methods of one or more classes

Consider the following code: class Rectangle: def __init__(self, height, width): self.height = height self.width = width def getPerimeter(self): perimeter = self.height * 2 + self.width * 2 return perimeter def getArea(self): area = self.height * self.width return area def getStr(self): return str(self.height) + " by " + str(self.width) If the Rectangle class is stored in a module named shapes, which of the following imports the class and creates an object from it?

from shapes import Rectangle rectangle = Rectangle(10, 20)

Consider the following code: class Multiplier: def __init__(self): self.num1 = 0 self.num2 = 0 def getProduct(self): return self.num1 * self.num2 def main(): m = Multiplier() m.num1 = 7 m.num2 = 3 print(m.num1, "X", m.num2, "=", m.getProduct()) if __name__ == "__main__": main() What is (are) the methods of the class?

getProduct()


Conjuntos de estudio relacionados

Retail Management Quiz Questions

View Set

AMH2010, Chapter 4, Slavery-Freedom-Struggle for Empire

View Set

PEDS Prep U Chapter 16: Nursing Care of the Child With an Alteration in Intracranial Regulation/Neurologic Disorder - ML3

View Set

Estate and Trust Planning Midterm

View Set

Personal Nutrition: Water-Soluble Vitamins

View Set