Expert Mastery — Series 3

Preview — 3 of 10 questions

What is the output of the following code?

javascript
class Dog:
    def speak(self):
        return "Woof"

class Cat:
    def speak(self):
        return "Meow"

d = Dog()
print(d.speak())
d.__class__ = Cat
print(d.speak())
print(isinstance(d, Cat))
print(isinstance(d, Dog))
AWoof, Meow, True, False
BTypeError: __class__ assignment only supported for heap types
CWoof, Meow, True, True
DWoof, Woof, False, True

What is the output of the following code?

javascript
def greet(self):
    return f"Hi, {self.name}"

Person = type("Person", (), {"greet": greet, "species": "human"})

p = Person()
p.name = "Ada"
print(p.greet())
print(Person.species)
print(type(Person))
AAttributeError: 'Person' object has no attribute 'greet'
BHi, Ada, human, <class 'type'>
CHi, Ada, human, <class 'Person'>
DTypeError: type() takes 1 or 3 arguments

What is the output of the following code?

javascript
class Foo:
    pass

try:
    f = Foo(1, 2, 3)
except TypeError as e:
    print("TypeError:", e)

class Bar:
    def __init__(self, x):
        self.x = x

b = Bar(5)
print(b.x)
ATypeError: Foo() takes no arguments, then TypeError: __init__() takes 2 positional arguments but 2 were given
BNo error for Foo(1, 2, 3) — arguments are stored on the instance automatically
CTypeError: Foo() takes no arguments, then 5
D1, then 5 — extra constructor arguments are silently discarded when __init__ isn't overridden

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.