0
0
Pythonprogramming~20 mins

Arithmetic operator overloading in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Operator Overloading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of adding two custom objects with overloaded + operator

Consider the following Python class that overloads the + operator. What will be the output of the code below?

Python
class Number:
    def __init__(self, value):
        self.value = value
    def __add__(self, other):
        return Number(self.value + other.value)
    def __str__(self):
        return str(self.value)

n1 = Number(5)
n2 = Number(10)
result = n1 + n2
print(result)
A15
BNumber object at some memory address
CTypeError
D510
Attempts:
2 left
💡 Hint

Look at how the __add__ method returns a new Number object and how __str__ controls printing.

Predict Output
intermediate
2:00remaining
Output of subtracting custom objects with overloaded - operator

What is the output of this code that overloads the - operator?

Python
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def __sub__(self, other):
        return Point(self.x - other.x, self.y - other.y)
    def __repr__(self):
        return f"Point({self.x}, {self.y})"

p1 = Point(10, 5)
p2 = Point(3, 2)
print(p1 - p2)
APoint(7, 3)
BPoint(13, 7)
CTypeError
DNone
Attempts:
2 left
💡 Hint

Check how the __sub__ method calculates the difference of coordinates.

Predict Output
advanced
2:00remaining
Output of multiplying custom objects with overloaded * operator returning int

What will this code print? The * operator is overloaded to return an integer.

Python
class Vector:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def __mul__(self, other):
        return self.x * other.x + self.y * other.y

v1 = Vector(2, 3)
v2 = Vector(4, 5)
print(v1 * v2)
ANone
BVector(8, 15)
CTypeError
D23
Attempts:
2 left
💡 Hint

The * operator here calculates the dot product of two vectors.

Predict Output
advanced
2:00remaining
Output of in-place addition with overloaded __iadd__ method

What is the output of this code that overloads the += operator?

Python
class Counter:
    def __init__(self, count=0):
        self.count = count
    def __iadd__(self, other):
        self.count += other.count
        return self
    def __str__(self):
        return str(self.count)

c1 = Counter(5)
c2 = Counter(10)
c1 += c2
print(c1)
ACounter object at some memory address
B15
CTypeError
D10
Attempts:
2 left
💡 Hint

The __iadd__ method modifies the object in place and returns it.

🧠 Conceptual
expert
2:00remaining
Behavior of unsupported arithmetic operator overloading

Given this class that only overloads __add__, what happens if you try to subtract two objects?

class MyNum:
    def __init__(self, val):
        self.val = val
    def __add__(self, other):
        return MyNum(self.val + other.val)

x = MyNum(5)
y = MyNum(3)
z = x - y
AReturns None
BReturns MyNum(2)
CRaises TypeError: unsupported operand type(s) for -: 'MyNum' and 'MyNum'
DRaises AttributeError
Attempts:
2 left
💡 Hint

Check what happens if an operator method is not defined for a class.