Peer Instruction: Classes Multiple Choice Questions¶
 open_window
 Incorrect! Method describes a specific action associated with a Class. "open_window" is a possbile action.
 accelerate
 Incorrect! Method describes a specific action associated with a Class. "accelerate" is a possbile action
 num_wheels
 Correct! "num_wheels" is a possible attribute which describes a specific feature.
 turn_right
 Incorrect! Method describes a specific action associated with a Class. "turn_right" is a possbile action.
 I don't know
 Incorrect! Method describes a specific action associated with a Class.
Q1: Which of the following is not a possible method for a Car
class?
 0 0
 Incorrect! Here, p2.x = 0 + 4 = 4 and p2.y = 0.
 4 0
 Correct! Here, p2.x = 0 + 4 = 4 and p2.y = 0.
 2 3
 Incorrect! Here, p2.x = 0 + 4 = 4 and p2.y = 0.
 7 3
 Incorrect! Here, p2.x = 0 + 4 = 4 and p2.y = 0.
 I don't know
 Incorrect! Here, p2.x = 0 + 4 = 4 and p2.y = 0.
Q2: What does this code output?
class Point:
def __init__ (self):
self.x = 0
self.y = 0
p1 = Point()
p1.x = p1.x + 2
p1.y = p1.x + 3
p2 = Point()
p2.x = p2.x + 4
print(p2.x, p2.y)
 do_it(d, e, f)
 Incorrect! You call a method using dot notation.
 do_it(self, d, e, f)
 Incorrect! You call a method using dot notation.
 do_it(t, d, e, f)
 Incorrect! You call a method using dot notation.
 t.do_it(d, e, f)
 Correct! Use dot notation on the object to call a method.
 t.do_it(t, d, e, f)
 Incorrect! Use dot notation and this will implicitly pass in the object as the first item, you don't also pass it in explicitly.
Q3: If t
is an object of class Thing
and d
, e
, and f
are defined, what is the proper way to call do_it
?
class Thing(object):
def do_it(self, a, b, c):
...
 20
 Incorrect! Here, a = 4, b = 5 and self.val = a*b = 20. So, '[' + str(20 + 2) + ']' = [22].
 [20]
 Incorrect! Here, a = 4, b = 5 and self.val = a*b = 20. So, '[' + str(20 + 2) + ']' = [22].
 22
 Incorrect! Here, a = 4, b = 5 and self.val = a*b = 20. So, '[' + str(20 + 2) + ']' = [22].
 [22]
 Correct! Here, a = 4, b = 5 and self.val = a*b = 20. So, '[' + str(20 + 2) + ']' = [22].
 I don't know
 Incorrect! Here, a = 4, b = 5 and self.val = a*b = 20. So, '[' + str(20 + 2) + ']' = [22].
Q4: What does this code output?
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)
 Incorrect! Account(50) assigns 50 to self.gold and when called again assigns 50 to other.gold too. So, the return statement evaluates to False (False and False).
 Account(80) == Account(90)
 Incorrect! Account(80) assigns 80 to self.gold and Account(90) assigns 90 to other.gold. So, the return statement evaluates to False (False and False).
 Account(0) == Account(5)
 Correct! Account(0) assigns 0 to self.gold and Account(5) assigns 5 to other.gold. So, the return statement evaluates to True.
 Account(0) == Account(0)
 Incorrect! Account(0) assigns 0 to self.gold and and when called again assigns 0 to other.gold too. So, the return statement evaluates to False (True and False).
 More than one of the above
 Incorrect! Account(0) assigns 0 to self.gold and Account(5) assigns 5 to other.gold. So, the return statement evaluates to True.
Q5: What does this code output?
class Account(object):
def __init__(self, val):
self.gold = val
def __eq__(self, other):
return self.gold==0 and other.gold==5
def __ne__(self, p): return not self == p

Correct! self == p calls “__eq__”. So, it essentially returns self != p.
def __ne__(self, p): return self.x == p.x and self.y == p.y

Incorrect! It would return the opposite.
def __ne__(self, p): if self.x =! p.x or self.y != p.y: return True return False

Correct! If self != p, then the “if” condition would evaluate to True returning True. Otherwise, it would return False.
I don’t know

Incorrect!
Q6: Which code for __ne__
is correct?
def __lt__(self, p): if self.x < p.x and self.y < p.y: return True return False

Incorrect! Consider negative numbers as well. For instance, (self.x = 1) > (p.x = 5) but self.x is near closer to the origin.
def __lt__(self, p): if self.magnitude() < p.magnitude(): return True return False

Correct! This option uses magnitude and thus the relation stands true for negative numbers as well.
def __lt__(self, p): my_val = math.sqrt(self.x**2 + self.y**2) p_val = math.sqrt(p.x**2 + p.y**2) if my_val < p_val: return True return False

Correct! The equation “math.sqrt(x**2 + y**2)” measures the absolute distance of point (x, y) from (0, 0).
I don’t know

Incorrect!
Q7: We want the point closer to the origin to be the lesser point. Which code is correct?
def __le__(self, p): if self < p or self == p: return True return False

Correct! self < p will invoke “__lt__” and self == p will invoke “__eq__”
def __le__(self, p): if self.magnitude() <= p.magnitude(): return True return False

Incorrect! This will lead to erroneous results in case of negative coordinates.
def __le__(self, p): if self.x <= p.x and self.y <= p.y: return True return False

Incorrect! This would have been correct if “and” is replaced by “or”
I don’t know

Incorrect! Option A is correct. self < p will invoke “__lt__” and self == p will invoke “__eq__”
Q8: Which implementation for (__le__)
is correct?