LS23 - Introduction to Object-oriented Programming
p0: Pasta = Pasta() p0.sauce = "marinara" p0.noodle = "spaghetti" p1: Pasta = Pasta() p1.sauce = "fettuccine" p1.noodle = "farfalle" print(p1.price()) In the method call to Pasta#price, self would contain a reference to the same Pasta object as which variable above? 1. p0 2. p1 3. No way to know
2
A class defines groupings of related variables called attributes. Each object, or instance, of a class has those attributes.
T
A class definition introduces a new data type, or type of object, in your program.
T
A method of a class is a special kind of function that all objects (instances) of the class will have available for calling.
T
Assume a class named Point. The following code listing results in a constructor call and a method call. a: Point = Point(1, -1) a.invert()
T
The constructor of a class is responsible for initializing the attributes of a new object (instance) before that object is used.
T
The first parameter of a constructor is self and it is given a reference to the newly constructed object on the heap
T
The first parameter of a method is self and it is given a reference to the object the method was called on.
T
The name of a constructor in a Python class definition is __init__.
T
You should assume there is an automatic statement at the end of each constructor which is: return self
T
Assume a class named Point. The following code listing results in a method call and not a constructor call. a: Point = Point(3, 4)
F
The constructor of a class is only called once in a program, no matter how many objects of that class are constructed.
F