Peer Instruction: Objects Multiple Choice QuestionsΒΆ
- 20
- Try again. str is a string combination of '[', str(self.val + 2) and ']'. self.val equals to 20, which is the result of a*b. Therefore, t is [22].
- [20]
- Try again. str is a string combination of '[', str(self.val + 2) and ']'. self.val equals to 20, which is the result of a*b. Therefore, t is [22].
- 22
- Try again. str is a string combination of '[', str(self.val + 2) and ']'. Therefore, t is a string that starts with '[' and ends with ']'. t is [22].
- [22]
- Correct! str is a string combination of '[', str(self.val + 2) and ']' and self.val equals to 20. Therefore, t equals to [22].
- A memory address
- Try again. str is a string combination of '[', str(self.val + 2) and ']'. self.val equals to 20, which is the result of a*b. Therefore, t is [22].
Q-1: What is the output of this code?
class Thing(object):
def __init__(self, a, b):
self.val = a * b
def __str__(self):
return '[' + str(self.val + 2) + ']'
t = Thing(4, 5)
print(t)
- Account(50) == Account(50)
- Try again. The equation only evaluates to True when self.gold equals to 0 and other.gold equals to 5. Account(50) == Account (50) returns False.
- Account(80) == Account(90)
- Try again. The equation only evaluates to True when self.gold equals to 0 and other.gold equals to 5. Account(80) == Account (90) returns False.
- Account(0) == Account(5)
- Correct! The equation only evaluates to True when self.gold equals to 0 and other.gold equals to 5. Account(0) == Account (5) returns True.
- Account(0) == Account(0)
- Try again. The equation only evaluates to True when self.gold equals to 0 and other.gold equals to 5. Account(0) == Account (0) returns False.
- More than one of the above
- Try again. There is only one right answer. The equation only evaluates to True when self.gold equals to 0 and other.gold equals to 5.
Q-2: Which of the following would evaluate to True?
class Account(object):
def __init__(self, val):
'''(int) -> Account
Create bank account with val gold.
'''
self.gold = val
def __eq__(self, other):
'''(Account) -> bool'''
return self.gold == 0 and other.gold == 5
- Account(50) == Account(50)
- Try again. The equation evaluates to True when self.gold equals to 0. As long as the first object in the equation equals to 0, the equation evaluates True.
- Account(80) == Account(90)
- Try again. The equation evaluates to True when self.gold equals to 0. As long as the first object in the equation equals to 0, the equation evaluates True.
- Account(0) == Account(5)
- Try again. There is more than one answer. The equation evaluates to True when self.gold equals to 0. As long as the first object in the equation equals to 0, the equation evaluates True.
- Account(0) == Account(0)
- Try again. There is more than one answer. The equation evaluates to True when self.gold equals to 0. As long as the first object in the equation equals to 0, the equation evaluates True.
- More than one of the above
- Correct! As long as the first object in the equation equals to 0, the equation evaluates True. Therefore, Account(0) == Account(0) and Account(0) == Account(5) are correct answers.
Q-3: Which of the following would evaluate to True?
class Account(object):
def __init__(self, val):
'''(int) -> Account
Create bank account with val gold.
'''
self.gold = val
def __eq__(self, other):
'''(Account) -> bool'''
return self.gold == 0
- Point(2, 3) < Point(4, 5)
- Try again. The equation evaluates to True when self.x < other.x or (self.x == other.x and self.y < other.y). Therefore, A and B are both right since self.x equals to 2 and other.x equals to 4.
- Point(2, 3) < Point(4, 1)
- Try again. The equation evaluates to True when self.x < other.x or (self.x == other.x and self.y < other.y). Therefore, A and B are both right since self.x equals to 2 and other.x equals to 4.
- Point(2, 3) < Point(1, 5)
- Try again. The equation evaluates to True when self.x < other.x or (self.x == other.x and self.y < other.y). C is incorrect because it does not satisfy either of the options.
- A and B
- Correct! The equation evaluates to True when self.x < other.x or (self.x == other.x and self.y < other.y). Therefore, A and B are both right since self.x equals to 2 and other.x equals to 4.
- All of the above
- Try again. The equation evaluates to True when self.x < other.x or (self.x == other.x and self.y < other.y). Therefore, A and B are both right since self.x equals to 2 and other.x equals to 4.
Q-4: Which of the following would evaluate to True?
class Point(object):
'''Two-dimensional points'''
def __init__ (self, x, y):
'''(int, int) -> Point
Create two-dimensional Point at (x, y)
'''
self.x = x
self.y = y
def __lt__(self, other):
return isinstance(other, Point) and (self.x < other.x \
or (self.x == other.x and self.y < other.y))
You have attempted of activities on this page