Unit 4: More on OOP concepts - Practice Quiz

ECAP776 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 What does function overloading generally mean?

Function overloading Easy
A. Calling one function repeatedly inside a loop
B. Using different function names with one parameter list
C. Using one function name with different parameter lists
D. Defining a function inside another function

2 Does Python directly support traditional function overloading based only on parameter lists?

Function overloading Easy
A. Yes, but only in classes
B. Yes, in every function
C. No, except inside loops
D. No, not in the traditional form

3 What happens when multiple functions with the same name are defined in the same Python scope?

Function overloading Easy
A. A syntax error always occurs
B. All definitions are combined
C. The first definition is retained
D. The latest definition replaces earlier ones

4 Which Python feature can imitate function overloading by allowing an argument to be omitted?

Function overloading Easy
A. Loop statements
B. Import statements
C. Default arguments
D. Global variables

5 Which parameter syntax allows a Python function to accept a variable number of positional arguments?

Function overloading Easy
A. *args
B. **args
C. @args
D. &args

6 What is a common benefit of function overloading?

Function overloading Easy
A. It converts all arguments into strings
B. It provides one name for related operations
C. It prevents functions from returning values
D. It removes every function parameter

7 Which standard-library decorator supports dispatch based on the type of the first argument?

Function overloading Easy
A. @classmethod
B. @staticmethod
C. @singledispatch
D. @property

8 What is operator overloading in Python?

Operator overloading Easy
A. Using several operators in one expression
B. Replacing an operator with a function name
C. Preventing operators from working with numbers
D. Giving an operator behavior for user-defined objects

9 Which special method overloads the + operator?

Operator overloading Easy
A. __sub__()
B. __eq__()
C. __add__()
D. __mul__()

10 Which special method overloads the - operator?

Operator overloading Easy
A. __div__()
B. __add__()
C. __mod__()
D. __sub__()

11 Which special method is associated with the == operator?

Operator overloading Easy
A. __lt__()
B. __ne__()
C. __gt__()
D. __eq__()

12 Which special method is called for the * operator?

Operator overloading Easy
A. __mod__()
B. __truediv__()
C. __matmul__()
D. __mul__()

13 What is another common name for methods such as __add__() and __sub__()?

Operator overloading Easy
A. Abstract methods
B. Dunder methods
C. Generator methods
D. Nested methods

14 If a + b is evaluated and a defines __add__(), which call does Python try?

Operator overloading Easy
A. b.__sub__(a)
B. a.__add__(b)
C. b.__add__(a)
D. a.__sub__(b)

15 Which built-in type demonstrates + with both arithmetic and concatenation behavior?

Operator overloading Easy
A. range
B. str
C. set
D. bool

16 What is method overriding?

Method overriding Easy
A. A function accepts a variable argument count
B. An object changes a built-in operator
C. A class defines two unrelated global functions
D. A child class redefines an inherited method

17 Which object-oriented concept is required for method overriding?

Method overriding Easy
A. Inheritance
B. Encapsulation
C. Recursion
D. Iteration

18 When an overridden method is called on a child-class object, which version normally runs?

Method overriding Easy
A. The parent-class version
B. The child-class version
C. The imported version
D. The global version

19 Which function can a child class use to call the parent class implementation?

Method overriding Easy
A. open()
B. super()
C. type()
D. input()

20 What does method overriding help a child class do?

Method overriding Easy
A. Disable object construction
B. Customize inherited behavior
C. Delete every parent attribute
D. Create multiple function names

21 What does the following code print?

def calculate(a, b):
return a + b

def calculate(a, b, c):
return a + b + c

print(calculate(2, 3))

Function overloading Medium
A. 5
B. None
C. 6
D. A TypeError is raised

22 Which definition best simulates overloading a function so that it can add either two or three numbers?

Function overloading Medium
A. def add(a, b, c): return a + b + c
B. def add(a=0): return a + a
C. def add(a, b, c=0): return a + b + c
D. def add(a, b): return a + b + b

23 What is the output of this code?

def total(*values):
return sum(values)

print(total(2, 4, 6))

Function overloading Medium
A. 12
B. 10
C. 6
D. 24

24 A function must format either one name or a first name followed by a last name. Which signature is most suitable?

Function overloading Medium
A. def format_name(first=None):
B. def format_name(first, last):
C. def format_name(*, first):
D. def format_name(first, last=None):

25 Given the function below, which call returns 6?

def multiply(a, b=2, c=1):
return a b c

Function overloading Medium
A. multiply(2, 2, 2)
B. multiply(a=1, c=3)
C. multiply(3, 3)
D. multiply(3)

26 Which approach provides type-based function dispatch using Python's standard library?

Function overloading Medium
A. Decorate a function with functools.singledispatch
B. Define the function repeatedly with new annotations
C. Decorate a function with staticmethod
D. Place each version in a separate conditional block

27 What does this function return for describe("Python")?

def describe(value):
if isinstance(value, str):
return len(value)
return value * 2

Function overloading Medium
A. PythonPython
B. 12
C. A TypeError is raised
D. 6

28 Which special method should a class implement to customize the behavior of the + operator?

Operator overloading Medium
A. __add__
B. __sum__
C. __plus__
D. __concat__

29 What does the following code print?

class Score:
def init(self, value):
self.value = value

def add(self, other):
return Score(self.value + other.value)

result = Score(7) + Score(5)
print(result.value)

Operator overloading Medium
A. 12
B. Score(12)
C. 2
D. 35

30 A Vector class should support 3 * vector when it already supports vector * 3 through __mul__. Which method should normally be added?

Operator overloading Medium
A. __radd__
B. __imul__
C. __rmul__
D. __matmul__

31 What should __add__ usually return when it cannot handle the type of the other operand?

Operator overloading Medium
A. None
B. NotImplemented
C. NotImplementedError
D. False

32 Which special method is invoked by a == b to test value equality?

Operator overloading Medium
A. __cmp__
B. __same__
C. __is__
D. __eq__

33 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])))

Operator overloading Medium
A. 3
B. 6
C. 5
D. 2

34 What is the main behavioral difference between __add__ and __iadd__?

Operator overloading Medium
A. __add__ is reflected, while __iadd__ is inherited
B. __add__ handles +, while __iadd__ handles +=
C. __add__ compares values, while __iadd__ adds values
D. __add__ handles +=, while __iadd__ handles +

35 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())

Method overriding Medium
A. bark
B. soundbark
C. A TypeError is raised
D. sound

36 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())

Method overriding Medium
A. Child
B. Child Parent
C. Parent Child
D. Parent

37 Which condition is essential for a subclass method to override an inherited instance method?

Method overriding Medium
A. It calls super first
B. It uses the same method name
C. It uses a different return type
D. It is marked with override

38 What does the following code print?

class Report:
def title(self):
return "General"

class SalesReport(Report):
pass

print(SalesReport().title())

Method overriding Medium
A. An AttributeError is raised
B. SalesReport
C. None
D. General

39 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()

Method overriding Medium
A. EmailNotification
B. default
C. Notification
D. email

40 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

Method overriding Medium
A. 8
B. 16
C. 2
D. 4

41 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))

Function overloading Hard
A. TypeError
B. 5 12
C. 5 7
D. 12 12

42 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))

Function overloading Hard
A. TypeError
B. 7 14
C. 14 77
D. 77 14

43 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))

Function overloading Hard
A. The generic handler, producing object
B. An ambiguity error between bool and int
C. An implicit Boolean handler, producing bool
D. The integer handler, producing int

44 What is the result of the final call?

from functools import singledispatch

@singledispatch
def combine(a, b):
return "base"

@combine.register(int)
def (a, b):
return "int:" + type(b).name

@combine.register(str)
def
(a, b):
return "str:" + type(b).name

print(combine(1, "x"))

Function overloading Hard
A. base
B. str:int
C. str:str
D. int:str

45 What does this program print?

from functools import singledispatchmethod

class Renderer:
@singledispatchmethod
def render(self, value, mode):
return "base"

@render.register
def _(self, value: int, mode):
return "int"

print(Renderer().render(True, "brief"))

Function overloading Hard
A. base
B. int
C. bool
D. TypeError

46 What is printed by the following class definition?

class Calculator:
def calc(self, x):
return x * 10

@staticmethod
def calc(x, y=1):
return x + y

print(Calculator().calc(3))

Function overloading Hard
A. 30
B. 3
C. 4
D. TypeError

47 What does this program print?

trace = []

class A:
def add(self, other):
trace.append("A.add")
return "left"

class B(A):
def radd(self, other):
trace.append("B.radd")
return "right"

print(A() + B(), trace)

Operator overloading Hard
A. right ['A.__add__', 'B.__radd__']
B. TypeError ['A.__add__']
C. left ['A.__add__']
D. right ['B.__radd__']

48 What is the outcome of x + x?

log = []

class X:
def add(self, other):
log.append("add")
return NotImplemented

def radd(self, other):
log.append("radd")
return 9

x = X()
try:
x + x
except TypeError:
print(log)

Operator overloading Hard
A. It returns 9 after logging ['radd']
B. It raises TypeError after logging ['add']
C. It raises TypeError after logging ['radd']
D. It returns 9 after logging ['add', 'radd']

49 What does the final statement print?

class Box:
def init(self, value):
self.value = value

def add(self, other):
return Box(self.value + other)

a = Box(2)
b = a
a += 3
print(a.value, b.value, a is b)

Operator overloading Hard
A. 2 2 False
B. 5 2 False
C. 5 2 True
D. 5 5 True

50 What happens when the final line executes?

class Key:
def init(self, value):
self.value = value

def eq(self, other):
return isinstance(other, Key) and self.value == other.value

k = Key(3)
print(hash(k))

Operator overloading Hard
A. It prints the same value as hash(3)
B. It raises AttributeError for a missing method
C. It prints the identity-based hash of k
D. It raises TypeError because k is unhashable

51 What is printed by this comparison?

log = []

class A:
def lt(self, other):
log.append("A.lt")
return "left"

class B(A):
def gt(self, other):
log.append("B.gt")
return "right"

print(A() < B(), log)

Operator overloading Hard
A. right ['A.__lt__', 'B.__gt__']
B. right ['B.__gt__']
C. TypeError []
D. left ['A.__lt__']

52 What are the values of events and result.tag?

events = []

class Token:
def init(self, tag):
self.tag = tag

def bool(self):
events.append("bool " + self.tag)
return False

class N:
def init(self, name):
self.name = name

def lt(self, other):
tag = self.name + other.name
events.append("lt " + tag)
return Token(tag)

result = N("a") < N("b") < N("c")

Operator overloading Hard
A. ['lt ab', 'lt bc'] and bc
B. ['lt ab', 'bool ab'] and ab
C. ['lt ab', 'bool ab', 'lt bc'] and bc
D. ['lt ab'] and ab

53 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())

Operator overloading Hard
A. False False
B. True True
C. True False
D. TypeError

54 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

Method overriding Hard
A. ['B', 'A']
B. ['B', 'A', 'C']
C. ['B', 'C', 'A']
D. ['C', 'A']

55 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()

Method overriding Hard
A. result is ['B', 'A']
B. result is ['B', 'C']
C. result is ['B', 'C', 'A']
D. result is ['C', 'B', 'A']

56 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()

Method overriding Hard
A. AttributeError because __step is private
B. sub because self has type Sub
C. sub because instance methods are virtual
D. base because the names are mangled separately

57 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}"

Method overriding Hard
A. B2/S2
B. B2/S1
C. B1/S1
D. B1/S2

58 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

def configure(self):
print(getattr(self, "ready", "missing"))

Sub()

Method overriding Hard
A. True
B. AttributeError
C. base
D. missing

59 What happens at s.x = 3?

class Base:
def init(self):
self._x = 1

@property
def x(self):
return self._x

@x.setter
def x(self, value):
self._x = value

class Sub(Base):
@property
def x(self):
return self._x * 10

s = Sub()
s.x = 3

Method overriding Hard
A. s._x remains silently unchanged
B. An AttributeError is raised
C. s.x becomes 30
D. s.x becomes 3

60 What is the outcome of the final expression?

from abc import ABC, abstractmethod

class A(ABC):
@abstractmethod
def f(self):
return "A"

class B(A):
def f(self):
return super().f() + "B"

result = B().f()

Method overriding Hard
A. AB because the abstract implementation is callable
B. A because the abstract method takes precedence
C. TypeError because super().f() remains abstract
D. B because abstract methods have no callable body