🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Magic Methods and Properties: all 10 questions

Quiz: Magic Methods and Properties · Question 5 of 10

What does this print? (code)

What does this print?

class V:
    def __init__(self, x):
        self.x = x
    def __add__(self, o):
        return V(self.x + o.x)
print((V(2) + V(5)).x)
  1. A.25
  2. B.7
  3. C.An error
  4. D.V(7)
Answer & explanation

Correct answer: 7

  • calls add, which returns a new V with x = 7.

Can you get the rest right without peeking?

Take the timed Quiz: Magic Methods and Properties quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →