ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
class K:
    def __new__(cls):
        print("new", end=" ")
        return 42
    def __init__(self):
        print("init", end=" ")
r = K()
print(r)
  1. A. new init 42
  2. B. new init <__main__.K object>
  3. C. new 42
  4. D. init 42

Correct answer: C. new 42

__init__ is called only if __new__ returns an instance of the class. Here __new__ returns the int 42, so __init__ is skipped and K() evaluates to 42.

More Python Programming questions

Exams that ask this topic