Every problem in the chapter beside the book’s answer, a chapter at a time as you scroll.
Chapter 11: Classes
38 problems · 0 figuresConcepts in Practice 11.1.1 §11.1 source ↗
- a.Ellis's followers list
- b.Ellis's username
Answer
Concepts in Practice 11.1.2 §11.1 source ↗
- a.program code
- b.real-world entity
Answer
Concepts in Practice 11.1.3 §11.1 source ↗
Venue field?- a.Add a procedure to change the field.
- b.Allow users direct access to the field.
- c.No valid way to allow changes to the field.
Answer
Change venue, can make changes to the field and protect the field from inappropriate access.Concepts in Practice 11.1.4 §11.1 source ↗
- a.All objects can easily access and modify each other's data.
- b.Each object's data is restricted for intentional access.
Answer
Concepts in Practice 11.1.5 §11.1 source ↗
- a.yes
- b.no
Answer
Concepts in Practice 11.1.6 §11.1 source ↗
- a.improved view of information
- b.high visibility of information
Answer
Concepts in Practice 11.2.1 §11.2 source ↗
Consider the example below:
class Cat:
def __init__(self):
self.name = 'Kitty'
self.breed = 'domestic short hair'
self.age = 1
def print_info(self):
print(self.name, 'is a ', self.age, 'yr old', self.breed)
pet_1 = Cat()
pet_2 = Cat() What is the name of the class?- a.
Cat - b.
class Cat - c.
self
Answer
CatCat follows the class keyword at the top of the class definition and is the name of the class.Concepts in Practice 11.2.2 §11.2 source ↗
Consider the example below:
class Cat:
def __init__(self):
self.name = 'Kitty'
self.breed = 'domestic short hair'
self.age = 1
def print_info(self):
print(self.name, 'is a ', self.age, 'yr old', self.breed)
pet_1 = Cat()
pet_2 = Cat() Which of the following is an instance of the Cat class?- a.
__init__ - b.
name - c.
pet_1
Answer
pet_1pet_1 is an instance of the Cat class created by calling Cat(). The other instance is pet_2.Concepts in Practice 11.2.3 §11.2 source ↗
Consider the example below:
class Cat:
def __init__(self):
self.name = 'Kitty'
self.breed = 'domestic short hair'
self.age = 1
def print_info(self):
print(self.name, 'is a ', self.age, 'yr old', self.breed)
pet_1 = Cat()
pet_2 = Cat() Which of the following is an attribute?- a.
breed - b.
pet_2 - c.
print_info
Answer
breedbreed is an instance attribute defined in __init__(). The other instance attributes are name and age.Concepts in Practice 11.2.4 §11.2 source ↗
Consider the example below:
class Cat:
def __init__(self):
self.name = 'Kitty'
self.breed = 'domestic short hair'
self.age = 1
def print_info(self):
print(self.name, 'is a ', self.age, 'yr old', self.breed)
pet_1 = Cat()
pet_2 = Cat() Suppose the programmer wanted to change the class to represent a pet cat. Which is the appropriate name that follows PEP 8 recommendations?- a.
petcat - b.
pet_cat - c.
PetCat
Answer
PetCatCapWords style, with all words capitalized and no spaces between words, is recommended for class names. "Pet" and "cat" are separate words, so each is capitalized and one directly follows the other.Concepts in Practice 11.2.5 §11.2 source ↗
Consider the example below:
1 class Rectangle:
2 def __init__(self):
3 self.length = 1
4 self.width = 1
5 def area(self):
6 return self.length * self.width
7
8 room_1 = Rectangle()
9 room_1.length = 10
10 room_1.width = 15
11 print("Room 1's area:", room_1.area())
12 room_3 = Rectangle()
13 room_3.length = 12
14 room_3.width = 14
15 print("Room 3's area:", room_3.area()) How many times is __init__() called?- a.1
- b.2
- c.3
Answer
room_1 and room_3, are created by calling Rectangle(), which calls the constructor __init__().Concepts in Practice 11.2.6 §11.2 source ↗
Consider the example below:
1 class Rectangle:
2 def __init__(self):
3 self.length = 1
4 self.width = 1
5 def area(self):
6 return self.length * self.width
7
8 room_1 = Rectangle()
9 room_1.length = 10
10 room_1.width = 15
11 print("Room 1's area:", room_1.area())
12 room_3 = Rectangle()
13 room_3.length = 12
14 room_3.width = 14
15 print("Room 3's area:", room_3.area()) When line 11 executes, execution flow moves to line 5. What does self represent on line 5?- a.the area of
room_1 - b.the instance
room_1 - c.the
Rectangleclass
Answer
room_1room_1 is the instance that called the method area(), so self represents the specific instance.Concepts in Practice 11.2.7 §11.2 source ↗
Consider the example below:
1 class Rectangle:
2 def __init__(self):
3 self.length = 1
4 self.width = 1
5 def area(self):
6 return self.length * self.width
7
8 room_1 = Rectangle()
9 room_1.length = 10
10 room_1.width = 15
11 print("Room 1's area:", room_1.area())
12 room_3 = Rectangle()
13 room_3.length = 12
14 room_3.width = 14
15 print("Room 3's area:", room_3.area()) Which line initializes the instance attribute length?- a.3
- b.6
- c.9
Answer
__init__(), which initializes length and the other instance attributes for each new instance.Concepts in Practice 11.2.8 §11.2 source ↗
Consider the example below:
1 class Rectangle:
2 def __init__(self):
3 self.length = 1
4 self.width = 1
5 def area(self):
6 return self.length * self.width
7
8 room_1 = Rectangle()
9 room_1.length = 10
10 room_1.width = 15
11 print("Room 1's area:", room_1.area())
12 room_3 = Rectangle()
13 room_3.length = 12
14 room_3.width = 14
15 print("Room 3's area:", room_3.area()) Suppose line 2 is changed to def __init__():. What would room_1's attributes be initialized to?- a.
length = 0,width = 0 - b.
length = 1,width = 1 - c.
Error
Answer
Error__init__() has one required parameter, conventionally called self, which allows the constructor to access the instance.Concepts in Practice 11.2.9 §11.2 source ↗
Consider the example above.
Which is an instance attribute?- a.
loc - b.
order_id - c.
order_3
Answer
order_idorder_id is an instance attribute defined in __init__(). The other instance attribute is cup_size.Concepts in Practice 11.2.10 §11.2 source ↗
Consider the example above.
Suppose the lineorder_1.cup_size = 8 is added before order_3.print_order(). What is the new output?- a.
Cafe Coffee Order 3 : 8 oz - b.
Cafe Coffee Order 3 : 16 oz - c.
Error
Answer
Cafe Coffee Order 3 : 16 ozorder_1.cup_size does not affect order_3.cup_size. order_1.cup_size changes to 8 and order_3.cup_size is still 16 .Concepts in Practice 11.2.11 §11.2 source ↗
Consider the example above.
Suppose the lineCoffeeOrder.loc = 'Caffeine Cafe' is added before order_3.print_order(). What is the new output?- a.
Caffeine Cafe Order 3 : 16 oz - b.
Cafe Coffee Order 3 : 16 oz - c.
Error
Answer
Caffeine Cafe Order 3 : 16 ozorder_1, order_2, and order_3 all share the class attribute CoffeeOrder.loc. Changing loc affects all instances, so Caffeine Cafe is printed as the location.Concepts in Practice 11.2.12 §11.2 source ↗
Consider the example above.
Suppose the lineself.cls_id = 5 is added to the end of __init__()'s definition. What is the new output?- a.
Cafe Coffee Order 5 : 16 oz - b.
Cafe Coffee Order 3 : 16 oz - c.
Error
Answer
Cafe Coffee Order 3 : 16 oz__init__(), trying to access the class attribute creates a new instance attribute called cls_id. Though possible, programmers should avoid using the same name for class attributes and instance attributes to avoid confusion.Concepts in Practice 11.3.1 §11.3 source ↗
Consider the example above.
Suppose the programmer wanted to make blood pressure a required parameter in__init__(). Which is the correct __init__() method header?- a.
def __init__(p_id, bp, self, tmp=-1.0, hr=-1, rr=-1): - b.
def __init__(self, p_id, bp, tmp=-1.0, hr=-1, rr=-1): - c.
def __init__(self, p_id, bp=[-1,-1], tmp=-1.0, hr=-1, rr=-1):
Answer
def __init__(self, p_id, bp, tmp=-1.0, hr=-1, rr=-1):bp a required parameter.Concepts in Practice 11.3.2 §11.3 source ↗
Consider the example above.
Which is a correct call to__init__() to create an instance with p_id=5241?- a.
patient_10 = Vitals(self, 5241) - b.
patient_10 = Vitals(5241) - c.both
Answer
patient_10 = Vitals(5241)self is the first parameter in __init__() definition, not an explicit argument. self should not be included in the call.Concepts in Practice 11.3.3 §11.3 source ↗
Consider the example above.
Suppose another__init__() definition is added after the first with the header as follows: def __init__(self, p_id, bp, tmp, hr, rr)What is the impact on the program?
- a.no change.
- b.Second
__init__()definition produces an error. - c.First two
__init__()calls produce an error.
Answer
__init__() calls produce an error.__init__() aren't allowed. __init__() requires all parameters, so the first two __init__() calls for patient_1 and patient_2 are incorrect.Concepts in Practice 11.3.4 §11.3 source ↗
Consider the example below:
class CoffeeOrder:
loc = 'Cafe Coffee'
cls_id = 1
def __init__(self, size=16, milk=False, sugar=False):
self.order_id = CoffeeOrder.cls_id
self.cup_size = size
self.with_milk = milk
self.with_sugar = sugar
CoffeeOrder.cls_id += 1
def change(self, milk, sugar):
self.with_milk = milk
self.with_sugar = sugar
def print_order(self):
print(CoffeeOrder.loc,'Order', self.order_id, ':', self.cup_size, 'oz')
if self.with_milk:
print('\twith milk')
if self.with_sugar:
print('\twith sugar')
order_1 = CoffeeOrder(8)
order_2 = CoffeeOrder(8, True, False)
order_1.change(False, True) What does order_1 represent at the end of the program?- a.8 oz coffee
- b.8 oz coffee with milk
- c.8 oz coffee with sugar
Answer
order_1 is originally a coffee without milk or sugar, but the call to change() adds sugar to the order by assigning order_1.with_sugar with True .Concepts in Practice 11.3.5 §11.3 source ↗
Consider the example below:
class CoffeeOrder:
loc = 'Cafe Coffee'
cls_id = 1
def __init__(self, size=16, milk=False, sugar=False):
self.order_id = CoffeeOrder.cls_id
self.cup_size = size
self.with_milk = milk
self.with_sugar = sugar
CoffeeOrder.cls_id += 1
def change(self, milk, sugar):
self.with_milk = milk
self.with_sugar = sugar
def print_order(self):
print(CoffeeOrder.loc,'Order', self.order_id, ':', self.cup_size, 'oz')
if self.with_milk:
print('\twith milk')
if self.with_sugar:
print('\twith sugar')
order_1 = CoffeeOrder(8)
order_2 = CoffeeOrder(8, True, False)
order_1.change(False, True) Why is change() an instance method?- a.can change attributes
- b.has multiple parameters
- c.can change an instance
Answer
change() allows a change in whether a coffee order has milk or sugar.Concepts in Practice 11.3.6 §11.3 source ↗
Consider the example below:
class CoffeeOrder:
loc = 'Cafe Coffee'
cls_id = 1
def __init__(self, size=16, milk=False, sugar=False):
self.order_id = CoffeeOrder.cls_id
self.cup_size = size
self.with_milk = milk
self.with_sugar = sugar
CoffeeOrder.cls_id += 1
def change(self, milk, sugar):
self.with_milk = milk
self.with_sugar = sugar
def print_order(self):
print(CoffeeOrder.loc,'Order', self.order_id, ':', self.cup_size, 'oz')
if self.with_milk:
print('\twith milk')
if self.with_sugar:
print('\twith sugar')
order_1 = CoffeeOrder(8)
order_2 = CoffeeOrder(8, True, False)
order_1.change(False, True) Can print_order() be unindented?- a.yes
- b.no
Answer
print_order() would produce an error. An instance method must be defined in the class.Concepts in Practice 11.4.1 §11.4 source ↗
- a.
add() - b.
_add_() - c.
__add__()
Answer
__add__()__add__() is a magic method, indicated by the double underscores at the start and end of the method name.Concepts in Practice 11.4.2 §11.4 source ↗
- a.can't be called by the user
- b.perform internal actions
- c.have fixed definitions
Answer
Concepts in Practice 11.4.3 §11.4 source ↗
- a.
__init__() - b.
__str__() - c.
__init__(),__str__()
Answer
__init__(), __str__()__init__() is defined by the programmer to initialize instance attributes. __str__() is defined by the programmer to customize an Engineer instance's string representation.Concepts in Practice 11.4.4 §11.4 source ↗
- a.overloading
- b.rewriting
- c.implementing
Answer
Concepts in Practice 11.4.5 §11.4 source ↗
other in the call to Account.__add__()? acct_a = Account("Ashe", 6492)
acct_b = Account("Bevins", 5210)
acct_ab = acct_a + acct_b- a.
acct_a - b.
acct_b - c.
acct_ab
Answer
acct_bacct_a, to the parameter self and the right operand, acct_b, to the parameter other.Concepts in Practice 11.4.6 §11.4 source ↗
__sub__() definition overloads the - operator for the code below to work? class Pair:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
# Define __sub__()
p1 = Pair(10, 2)
p2 = Pair(8, 4)
p3 = p1 - p2
print(p3.x, p3.y)- a.
def __sub__(self): return Pair(self.x - other.x, self.y - other.y)
- b.
def __sub__(self, other): return self.x - other.x, self.y - other.y
- c.
def __sub__(self, other): return Pair(self.x - other.x, self.y - other.y)
Answer
def __sub__(self, other): return Pair(self.x - other.x, self.y - other.y)
__sub__() has two parameters. The parameters' x and y attributes are subtracted from each other, and the resulting values are made into a new Pair.Concepts in Practice 11.4.7 §11.4 source ↗
Consider the example above.
How many operators are overloaded?- a.3
- b.4
- c.5
Answer
__lt__(), __gt__(), and __eq__(), respectively.Concepts in Practice 11.4.8 §11.4 source ↗
Consider the example above.
Which code appropriately overloads the <= operator?- a.
def __le__(other): return self.amount <= other.amount
- b.
def __le__(self, other): return self.amount <= other.amount
- c.
def __le__(self, other): return other.amount <= self.amount
Answer
def __le__(self, other): return self.amount <= other.amount
__le__() is the magic method for the <= operator. __le__() takes two parameters for the left and right operand, and returns the correct comparison of the two amount values.Concepts in Practice 11.4.9 §11.4 source ↗
Consider the example above.
Which type of value does__gt__() return?- a.account instance
- b.Boolean
- c.integer
Answer
__gt__() overloads the > operator, which makes a comparison and returns True or False .Concepts in Practice 11.5.1 §11.5 source ↗
- a.1
- b.2
- c.3
Answer
Level and XP are classes imported from the character module into the program. Any other class in the character module is not imported.Concepts in Practice 11.5.2 §11.5 source ↗
Level class from character?- a.
import Level from character - b.
from character import Level - c.
import Level
Answer
from character import Levelname(s).Concepts in Practice 11.5.3 §11.5 source ↗
- a.none
- b.only 1
- c.any number
Answer
Concepts in Practice 11.5.4 §11.5 source ↗
Consider the example above. Suppose a program imports the character module using import character as game_char.
Level instance?- a.
rogue_level = character.Level() - b.
rogue_level = game_char.Level() - c.
rogue_level = character.game_char.Level()
Answer
rogue_level = game_char.Level()game_char, so character is not defined in the program. game_char is used in place of character.Concepts in Practice 11.5.5 §11.5 source ↗
Consider the example above. Suppose a program imports the character module using import character as game_char.
- a.
character = 'Ranger' - b.
Game_char = 'Ranger' - c.
game_char = 'Ranger'
Answer
game_char = 'Ranger'game_char referred to the imported module, but assigning game_char replaces the imported module with the string 'Ranger' . game_char can no longer be used to access classes.Loading chapter 12…
Problems and answers are adapted from Introduction to Python Programming by Udayan Das, Aubrey Lawson, Chris Mayfield and Narges Norouzi, OpenStax, and are used under the CC BY-NC-SA 4.0 licence. Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction. Illustrations: Copyright Rice University, OpenStax, under CC BY-NC-SA 4.0 license. This adaptation is released under the same licence, is non-commercial, and is neither affiliated with nor endorsed by OpenStax or Rice University.