Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.
You're offline
Unit 4 MCQsMore on OOP concepts
ECAP776 ·
60 questions. Pick an option to check it.
__add__ creates a new Score whose value is the sum of the two operand values.
Question 30 of 60 · Operator overloading · Medium
A Vector class should support 3 * vector when it already supports vector * 3 through __mul__. Which method should normally be added?
Not quite. Try another option.
Answer: __rmul__
__rmul__ handles reflected multiplication when the custom object appears on the right.
Question 31 of 60 · Operator overloading · Medium
What should __add__ usually return when it cannot handle the type of the other operand?
Not quite. Try another option.
Answer: NotImplemented
Returning NotImplemented lets Python try reflected operations or raise an appropriate TypeError.
Question 32 of 60 · Operator overloading · Medium
Which special method is invoked by a == b to test value equality?
Not quite. Try another option.
Answer: __eq__
The equality operator delegates to __eq__; is checks identity and cannot be overloaded.
Question 33 of 60 · Operator overloading · Medium
What is printed by the following code?
class Box:
def init(self, items):
self.items = items
def len(self):
return len(self.items) * 2
print(len(Box([1, 2, 3])))
Not quite. Try another option.
Answer: 6
len calls __len__, which doubles the underlying list length of .
Question 34 of 60 · Operator overloading · Medium
What is the main behavioral difference between __add__ and __iadd__?
Not quite. Try another option.
Answer: __add__ handles +, while __iadd__ handles +=
__iadd__ customizes augmented assignment; if unavailable, Python can fall back to __add__.
Question 35 of 60 · Method overriding · Medium
What does this code print?
class Animal:
def speak(self):
return "sound"
class Dog(Animal):
def speak(self):
return "bark"
pet = Dog()
print(pet.speak())
Not quite. Try another option.
Answer: bark
Dog.speak overrides the inherited method, so it is selected for a Dog instance.
Question 36 of 60 · Method overriding · Medium
What is printed by this code?
class Parent:
def message(self):
return "Parent"
class Child(Parent):
def message(self):
return super().message() + " Child"
print(Child().message())
Not quite. Try another option.
Answer: Parent Child
super().message() obtains the parent result, after which the child method appends its own text.
Question 37 of 60 · Method overriding · Medium
Which condition is essential for a subclass method to override an inherited instance method?
Not quite. Try another option.
Answer: It uses the same method name
Python resolves attributes by name, so a subclass method with the inherited method's name replaces it for subclass instances.
Question 38 of 60 · Method overriding · Medium
What does the following code print?
class Report:
def title(self):
return "General"
class SalesReport(Report):
pass
print(SalesReport().title())
Not quite. Try another option.
Answer: General
SalesReport does not override title, so method lookup finds the inherited implementation.
Question 39 of 60 · Method overriding · Medium
Consider the classes below. What does process(EmailNotification()) return?
class Notification:
def send(self):
return "default"
class EmailNotification(Notification):
def send(self):
return "email"
def process(item):
return item.send()
Not quite. Try another option.
Answer: email
Dynamic dispatch selects the overridden send method based on the object's runtime class.
Question 40 of 60 · Method overriding · Medium
What is the result of calling Manager().work()?
class Employee:
def work(self, hours=4):
return hours
class Manager(Employee):
def work(self, hours=4):
return super().work(hours) * 2
Not quite. Try another option.
Answer: 8
The manager passes the default value to the parent implementation and then doubles its result.
Question 41 of 60 · Function overloading · Hard
What does the following program print?
def f(x):
return x + 1
g = f
def f(x, y=3):
return x * y
print(g(4), f(4))
Not quite. Try another option.
Answer: 5 12
The second definition rebinds f rather than overloading it. However, g still refers to the original one-argument function.
Question 42 of 60 · Function overloading · Hard
What is printed at runtime?
from typing import overload
@overload
def convert(x: int) -> str: ...
@overload
def convert(x: str) -> int: ...
def convert(x):
return x * 2
print(convert("7"), convert(7))
Not quite. Try another option.
Answer: 77 14
@overload declarations are for static type checkers. Runtime calls use only the final implementation, so string and integer multiplication produce "77" and 14.
Question 43 of 60 · Function overloading · Hard
Which handler is selected by this singledispatch call?
from functools import singledispatch
@singledispatch
def label(x):
return "object"
@label.register(int)
def _(x):
return "int"
print(label(True))
Not quite. Try another option.
Answer: The integer handler, producing int
singledispatch searches the runtime type's MRO. Since bool is a subclass of int, the registered int implementation applies.
The first comparison returns a Token, which is tested for truth before continuing the chain. Its false value short-circuits the second comparison, and that token becomes the result.
Question 53 of 60 · Operator overloading · Hard
What does the following program print?
class Bag:
def getitem(self, index):
if index >= 3:
raise IndexError
return (4, 6, 8)[index]
print(6 in Bag(), 5 in Bag())
Not quite. Try another option.
Answer: True False
With neither __contains__ nor __iter__, membership uses the old sequence protocol, calling __getitem__ from index 0 until a match or IndexError.
Question 54 of 60 · Method overriding · Hard
What does D().m() return under Python's method resolution order?
class A:
def m(self):
return ["A"]
class B(A):
def m(self):
return ["B"] + super().m()
class C(A):
def m(self):
return ["C"] + super().m()
class D(B, C):
pass
Not quite. Try another option.
Answer: ['B', 'C', 'A']
The MRO is D, B, C, A, object. Cooperative super() calls therefore visit B, then C, then A.
Question 55 of 60 · Method overriding · Hard
How does changing C.m affect the result?
class A:
def m(self):
return ["A"]
class B(A):
def m(self):
return ["B"] + super().m()
class C(A):
def m(self):
return ["C"]
class D(B, C):
pass
result = D().m()
Not quite. Try another option.
Answer: result is ['B', 'C']
B.m delegates to C.m through the MRO, but C.m does not call super(). The cooperative chain therefore stops before reaching A.m.
Question 56 of 60 · Method overriding · Hard
What is returned by Sub().call()?
class Base:
def call(self):
return self.step()
def step(self):
return "base"
class Sub(Base):
def __step(self):
return "sub"
result = Sub().call()
Not quite. Try another option.
Answer: base because the names are mangled separately
Base.__step is compiled as _Base__step, while Sub.__step becomes _Sub__step. The subclass method does not override the mangled base method.
Question 57 of 60 · Method overriding · Hard
What does Sub().f() return?
class Base:
def f(self, x=1):
return f"B{x}"
class Sub(Base):
def f(self, x=2):
return super().f() + f"/S{x}"
Not quite. Try another option.
Answer: B1/S2
The call to Sub.f uses its default x=2. The separate call to Base.f through super() supplies no argument, so Base.f uses its own default x=1.
Question 58 of 60 · Method overriding · Hard
What does constructing Sub() print?
class Base:
def init(self):
self.configure()
def configure(self):
print("base")
class Sub(Base):
def init(self):
super().init()
self.ready = True
Nearly through. Did unit 4 of ECAP776 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 4 of ECAP776 save you a night before the exam?
Then you can help keep it free for the next batch. Ads cover part of the server bill;
the rest comes out of a student's own pocket: the domain, the storage, and keeping
the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason.
to try again.