Unit 4 subjective questions More on OOP concepts
ECAP776 20 questions with model answers. Write yours first, then compare.
-
Question 1 of 20
Define function overloading. Does Python support traditional function overloading based on parameter types or parameter count?
Model answer
Function overloading means defining multiple functions with the same name but different parameter lists so that the appropriate version is selected based on the arguments.
Python does not support traditional compile-time function overloading. If multiple functions with the same name are defined in the same scope, the latest definition replaces the earlier definitions.
For example:
def display(value): return str(value) def display(value, prefix): return prefix + str(value)Only the second definition remains available. Calling
display(10)therefore raises aTypeError.Python achieves similar behavior using:
- Default arguments
- Variable-length arguments such as
*argsand**kwargs - Type inspection
functools.singledispatch
Thus, Python provides flexible alternatives rather than traditional signature-based overloading.
-
Question 2 of 20
Explain how default arguments can be used to simulate function overloading in Python, with an example.
Model answer
Default arguments allow a function to accept different numbers of arguments by assigning initial values to optional parameters.
Example:
def area(length, breadth=None): if breadth is None: return length * length return length * breadthIn this function:
area(5)treats the shape as a square and returns25.area(5, 3)treats the shape as a rectangle and returns15.
The single function therefore performs different operations depending on the number of supplied arguments. This resembles function overloading, although Python is actually executing one function definition with an optional parameter.
Using
Noneas the default value is often safer than using a meaningful numeric value because it clearly indicates that the argument was omitted. -
Question 3 of 20
Describe how
*argsand**kwargshelp implement flexible or overloaded-like functions in Python.Model answer
*argsand**kwargsallow a function to receive a variable number of arguments.*argscollects extra positional arguments into a tuple.**kwargscollects extra keyword arguments into a dictionary.
Example:
def calculate(*args, **kwargs): operation = kwargs.get("operation", "sum") if operation == "sum": return sum(args) if operation == "product": result = 1 for value in args: result *= value return result raise ValueError("Unsupported operation")Usage:
calculate(2, 3, 4) calculate(2, 3, 4, operation="product")The function can process different numbers of values and select behavior through a keyword. This provides overloaded-like flexibility, but the programmer must validate the arguments and handle unsupported combinations explicitly.
-
Question 4 of 20
Compare the use of default arguments,
*args, and explicit type checking as alternatives to function overloading in Python.Model answer
Python offers several alternatives to traditional function overloading:
- Default arguments: Best when the operation has a fixed set of optional parameters. They produce clear function signatures but may become complicated when many combinations are permitted.
*argsand**kwargs: Best when the number of arguments is variable or not known in advance. They are highly flexible, but the accepted interface may be less obvious and requires manual validation.- Explicit type checking: Uses tools such as
isinstance()to select behavior according to argument types. It can be useful for a small number of cases but may produce long conditional structures and tightly coupled code.
For example, a function may use
isinstance(value, str)to process text differently from numbers. However, excessive type checking conflicts with Python's preference for polymorphism and duck typing.The preferred technique depends on whether behavior varies by omitted parameters, argument quantity, or argument type.
-
Question 5 of 20
What is
functools.singledispatch? Explain how it provides type-based function dispatch in Python.Model answer
functools.singledispatchconverts a normal function into a generic function whose implementation is selected according to the type of its first argument.Example:
from functools import singledispatch @singledispatch def describe(value): return f"Object: {value}" @describe.register def _(value: int): return f"Integer: {value}" @describe.register def _(value: list): return f"List with {len(value)} items"Key points:
- The undecorated-style base implementation acts as the fallback.
- Registered implementations handle specified types.
- Dispatch considers the first argument's runtime type and its inheritance hierarchy.
- It is runtime dispatch, not traditional compile-time overloading.
For methods, Python also provides
functools.singledispatchmethod, which applies a similar idea while ignoringselforclswhen choosing the implementation. -
-
Question 6 of 20
Explain operator overloading in Python. Why is it useful in user-defined classes?
Model answer
Operator overloading is the process of defining how built-in operators behave when applied to objects of a user-defined class. Python implements this through special methods, also called dunder methods.
Examples include:
__add__()for+__sub__()for-__mul__()for*__eq__()for==__lt__()for<
If
a + bis evaluated, Python generally attempts to calla.__add__(b).Operator overloading is useful because it:
- Gives domain objects natural and readable syntax.
- Allows objects to interact with Python operators.
- Supports polymorphic behavior.
- Makes classes such as vectors, matrices, dates, and monetary values easier to use.
Operators should retain intuitive meanings. For example,
+should normally combine or add objects rather than perform an unrelated task. -
Question 7 of 20
Design a
Vector2Dclass that overloads the+operator. Explain what happens when two vector objects are added.Model answer
A two-dimensional vector may overload
+by defining__add__():class Vector2D: def __init__(self, x, y): self.x = x self.y = y def __add__(self, other): if not isinstance(other, Vector2D): return NotImplemented return Vector2D(self.x + other.x, self.y + other.y) def __repr__(self): return f"Vector2D({self.x}, {self.y})"Example:
first = Vector2D(2, 3) second = Vector2D(4, 5) result = first + secondPython translates the addition approximately into
first.__add__(second). The method adds corresponding components, so the result isVector2D(6, 8).Returning a new object avoids unexpectedly modifying either operand. Returning
NotImplementedfor an unsupported type also allows Python to try a reflected operation before eventually raisingTypeError. -
Question 8 of 20
Distinguish among
__add__(),__radd__(), and__iadd__()in Python operator overloading.Model answer
These methods represent three related forms of addition:
__add__(self, other)handles ordinary addition such asa + b, withaas the left operand.__radd__(self, other)handles reflected addition. Python may tryb.__radd__(a)whena.__add__(b)is unavailable or returnsNotImplemented, subject to subclass dispatch rules.__iadd__(self, other)handles augmented assignment such asa += b.
__iadd__()may mutate and returnself, which is common for mutable objects. It may instead return a new object, which is appropriate for immutable objects. If__iadd__()is unavailable or returnsNotImplemented, Python normally falls back to ordinary addition and assignment.These methods permit classes to support left-hand, right-hand, and in-place forms of the same operator while correctly handling mixed operand types.
-
Question 9 of 20
Explain the purpose of returning
NotImplementedfrom an overloaded operator method. How does it differ from raisingNotImplementedError?Model answer
NotImplementedis a special singleton value that an operator method should return when it does not support the other operand's type.For an expression such as
left + right:- Python may first call
left.__add__(right). - If that method returns
NotImplemented, Python may try the corresponding reflected method,right.__radd__(left). - If neither operand supports the operation, Python raises
TypeError.
NotImplementedError, by contrast, is an exception generally raised by methods whose implementation is intentionally absent, often in an incomplete base-class interface.Therefore:
- Use
return NotImplementedfor unsupported operand combinations in binary operator methods. - Use
raise NotImplementedErroronly to indicate that a method itself must be implemented or has not yet been implemented.
Raising
NotImplementedErrorinside an operator method prevents Python's normal reflected-operation negotiation. - Python may first call
-
Question 10 of 20
Describe how comparison operators can be overloaded in Python. Include the roles of
__eq__()and__lt__().Model answer
Comparison operators are overloaded using special methods:
__eq__()implements==.__ne__()implements!=.__lt__()implements<.__le__()implements<=.__gt__()implements>.__ge__()implements>=.
Example:
class Student: def __init__(self, name, score): self.name = name self.score = score def __eq__(self, other): if not isinstance(other, Student): return NotImplemented return self.name == other.name and self.score == other.score def __lt__(self, other): if not isinstance(other, Student): return NotImplemented return self.score < other.scoreHere, equality compares both attributes, while
<orders students by score. Defining comparison methods enables operations such as equality tests and sorting. The chosen comparison rules should be consistent and clearly documented. -
Question 11 of 20
What is the relationship between
__eq__()and__hash__()when operator overloading is used for value equality?Model answer
__eq__()defines when two objects are considered equal, while__hash__()supplies the integer hash used by hash-based collections such as dictionaries and sets.The essential rule is:
- If
a == b, thenhash(a)must equalhash(b).
When a class defines
__eq__()but does not provide a compatible__hash__(), Python commonly makes instances unhashable by setting__hash__toNone. This prevents mutable or value-equal objects from corrupting set and dictionary behavior.A suitable implementation for an immutable object is:
def __hash__(self): return hash((self.x, self.y))The same attributes used by
__eq__()should normally be used to calculate the hash. Attributes involved in hashing must not change while the object is stored in a dictionary or set. - If
-
Question 12 of 20
Explain how the
@functools.total_orderingdecorator reduces the work needed to overload ordering operators.Model answer
@functools.total_orderingis a class decorator that generates missing ordering methods from a smaller set of explicitly defined methods.To use it, a class must define:
__eq__(), and- At least one ordering method, such as
__lt__(),__le__(),__gt__(), or__ge__().
Example:
from functools import total_ordering @total_ordering class Item: def __init__(self, price): self.price = price def __eq__(self, other): if not isinstance(other, Item): return NotImplemented return self.price == other.price def __lt__(self, other): if not isinstance(other, Item): return NotImplemented return self.price < other.pricePython derives the other ordering operations from these definitions. This reduces duplication, although explicitly implementing all methods can provide faster execution and clearer stack traces in performance-sensitive code.
-
Question 13 of 20
Differentiate between operator overloading and method overriding in Python.
Model answer
Operator overloading and method overriding are different forms of polymorphism.
Operator overloading:
- Defines operator behavior for class objects.
- Uses special methods such as
__add__(),__mul__(), and__eq__(). - Example: defining how two
Vectorobjects behave with+.
Method overriding:
- Occurs when a subclass supplies its own implementation of a method inherited from a base class.
- Uses the same method name and a compatible calling contract.
- Example: a
Dogclass replacing an inheritedspeak()implementation.
Operator overloading focuses on expressions involving operators, whereas method overriding focuses on inherited behavior. Both support polymorphism because the executed operation depends on the runtime type of the participating object.
-
Question 14 of 20
Define method overriding and illustrate it using a base class and a derived class.
Model answer
Method overriding occurs when a subclass defines a method with the same name as a method inherited from its base class, replacing or extending the inherited behavior for subclass instances.
Example:
class Animal: def speak(self): return "Some sound" class Dog(Animal): def speak(self): return "Bark" animal = Animal() dog = Dog()animal.speak()returns"Some sound", whiledog.speak()returns"Bark".When
speak()is called on aDogobject, Python searches theDogclass beforeAnimal, so it finds the overridden implementation first. This enables runtime polymorphism: different object types can respond differently to the same method call while sharing a common interface. -
Question 15 of 20
Explain the role of
super()in an overridden method. Why is it preferable to directly naming the parent class in many situations?Model answer
super()returns a proxy that delegates method lookup to the next class in the method resolution order (MRO). It is commonly used when an overriding method must extend rather than completely replace inherited behavior.Example:
class Employee: def __init__(self, name): self.name = name class Manager(Employee): def __init__(self, name, department): super().__init__(name) self.department = departmentBenefits of
super()include:- Avoiding hard-coded references to a particular parent class.
- Supporting cooperative multiple inheritance.
- Following the class's MRO correctly.
- Making code easier to maintain if the inheritance hierarchy changes.
Calling
Employee.__init__(self, name)may work in simple single inheritance, but it bypasses cooperative MRO behavior and can cause a class to be called twice or skipped in multiple-inheritance hierarchies. -
Question 16 of 20
Explain how Python's method resolution order (MRO) affects method overriding in multiple inheritance.
Model answer
The method resolution order specifies the sequence in which Python searches classes for an attribute or method. In multiple inheritance, Python uses the C3 linearization algorithm to produce a consistent order that respects inheritance relationships and local parent ordering.
Example:
class A: def show(self): return "A" class B(A): def show(self): return "B" class C(A): def show(self): return "C" class D(B, C): passFor
D, the MRO is normallyD, B, C, A, object. Therefore,D().show()findsB.show()first and returns"B".The MRO can be inspected with
D.mro()orD.__mro__. A zero-argumentsuper()advances to the next class in this order, not necessarily to a class's immediate textual parent. This is essential for cooperative multiple inheritance. -
Question 17 of 20
Describe cooperative method overriding in multiple inheritance and state the conditions required for it to work correctly.
Model answer
In cooperative method overriding, each class performs its own work and then uses
super()to pass control to the next implementation in the MRO.Example:
class Root: def process(self): return ["Root"] class Left(Root): def process(self): return ["Left"] + super().process() class Right(Root): def process(self): return ["Right"] + super().process() class Child(Left, Right): def process(self): return ["Child"] + super().process()Child().process()follows the MRO and returns entries fromChild,Left,Right, andRootonce each.For this pattern to work correctly:
- Every participating implementation should call
super(). - Method signatures must be mutually compatible.
- Each method should consume its own arguments and forward the remaining arguments when necessary.
- A terminating implementation must exist.
Directly calling a named parent can break this chain.
- Every participating implementation should call
-
Question 18 of 20
How do abstract methods relate to method overriding? Explain using the
abcmodule.Model answer
An abstract method declares behavior that subclasses are expected to implement through method overriding. Python provides abstract base classes through the
abcmodule.Example:
from abc import ABC, abstractmethod class Shape(ABC): @abstractmethod def area(self): pass class Rectangle(Shape): def __init__(self, length, breadth): self.length = length self.breadth = breadth def area(self): return self.length * self.breadthShapecannot normally be instantiated becausearea()is abstract.Rectanglebecomes concrete by overridingarea().Abstract methods:
- Define a required interface.
- Improve consistency among subclasses.
- Allow polymorphic use through a common base class.
- May contain an implementation that an overriding method can call with
super().
If a subclass fails to implement all required abstract methods, that subclass also remains abstract.
-
Question 19 of 20
Compare method overriding with simply adding a new method to a subclass. Discuss interface compatibility.
Model answer
When a subclass overrides a method, it replaces an inherited implementation while preserving the conceptual operation represented by that method. Adding a new method introduces behavior that was not part of the inherited interface.
Example:
- Overriding
draw()inCirclechanges how a generalShapeis drawn. - Adding
calculate_diameter()toCircleintroduces circle-specific behavior.
A good override should honor the base method's contract:
- Accept calls that validly target the base method.
- Return an appropriate kind of result.
- Preserve documented expectations and invariants.
- Avoid imposing surprising new requirements.
This compatibility supports substitutability: code written for the base class should continue to work when given a subclass object. An unrelated new method does not affect calls through the base interface and is available only to code aware of the subclass capability.
- Overriding
-
Question 20 of 20
Develop a Python example that combines method overriding and operator overloading, and explain how both forms of polymorphism operate.
Model answer
The following example combines overridden methods with an overloaded operator:
class Shape: def area(self): raise NotImplementedError def __lt__(self, other): if not isinstance(other, Shape): return NotImplemented return self.area() < other.area() class Rectangle(Shape): def __init__(self, length, breadth): self.length = length self.breadth = breadth def area(self): return self.length * self.breadth class Circle(Shape): def __init__(self, radius): self.radius = radius def area(self): return 3.14159 * self.radius ** 2 rectangle = Rectangle(4, 5) circle = Circle(3) result = rectangle < circleMethod overriding:
RectangleandCircleprovide different implementations ofarea(). The implementation is selected according to the runtime type of each object.Operator overloading:
Shape.__lt__()defines the meaning of<for shapes. It invokes the overriddenarea()methods and compares their results.This design demonstrates runtime polymorphism: one shared comparison implementation works with multiple subclasses because each subclass supplies its own area calculation.
That's unit 4
Long answers stick best when they're tested again soon. Run the MCQs on this unit, then come back to the questions you found hardest.
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.
Nothing here is ever locked, and nothing unlocks.