CMU 15-112: Fundamentals of Programming and Computer Science
Writing-Session9 Practice
- These exercises will help you prepare for writing-session9,
which is on Thu 24-Oct in lecture, and which will contain a randomly-chosen
subset of exercises from among these.
- Unlike the hw, you may work collaboratively on these practice exercises.
- That said, during the actual writing session on Friday you
must work alone, and without any notes or access to the web or other
resources.
1. Which method do we write when we need to compare the equality
of two objects?
A) __str__ B) __eq__ C) __repr__ D) __hash__
2. Which method do we write to tell Python how to convert our objects
to a printable string, whether they are inside a list or not?
A) __str__ B) __eq__ C) __repr__ D) __hash__
3. Which methods must we write if we need to store an object in a list
or dictionary (hint: we need to write two methods in this case):
A) __str__ B) __eq__ C) __repr__ D) __hash__
4. What term describes values in a class that are shared by all instances
of that class?
A) class attributes B) static methods C) static attributes D) class methods
5. Which of the following statements about static methods is True?
A) We must call them on an object.
B) We may call them on a class.
C) Subclasses cannot override a superclass's static methods.
D) The line "@staticmethod" is just a special way of commenting OOP
and does not actually do anything.
6. When we write a subclass that overrides a method from its superclass:
A) Instances of the subclass cannot call the method
B) We specify a new behavior for that method for instances of the subclass
C) The subclass inherits the method from the superclass
D) The superclass inherits the method from the subclass
7. Which of the following would be the best hashable values
for Cat? (select two)
A) self.name
B) self.isHungry
C) self.birthday
D) self.age
E) self.isSleeping
Fill in the blank with the most likely choice:
subclass, superclass, instance, method
8. Dog is most likely a(n) ______________ of Pet.
9. myDog is most likely a(n) ______________ of Dog and a(n) ______________ of Pet
10. Animal is most likely a(n) ______________ of Pet.
11. Given the following code:
class A(object): pass
class B(A): pass
a = A()
b = B()
Will the following lines return True or False (circle your answers)
isinstance(a, A) # True or False
isinstance(a, B) # True or False
isinstance(b, A) # True or False
isinstance(b, B) # True or False
type(a) == type(A) # True or False
type(a) == type(B) # True or False
type(b) == type(A) # True or False
type(b) == type(B) # True or False
a == A # True or False
b == B # True or False