ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
class Celsius:
    def __init__(self, t=0): self.t = t
    def __get__(self, obj, owner):
        return self if obj is None else obj._c
    def __set__(self, obj, v):
        obj._c = round(v, 1)
class Room:
    temp = Celsius()
r1, r2 = Room(), Room()
r1.temp = 21.66
r2.temp = 19
print(r1.temp, r2.temp, type(Room.temp).__name__)
  1. A. 21.66 19 Celsius
  2. B. 19 19 Celsius
  3. C. 21.7 19 Celsius
  4. D. 21.7 19 float

Correct answer: C. 21.7 19 Celsius

The descriptor instance is shared by the class, but it stores each value on the owning object (obj._c), so rooms do not interfere. Accessed through the class (obj is None) it returns itself.

More Python Programming questions

Exams that ask this topic