ProgrammingHardAccenture pattern

What is printed?

What is printed?
class Temperature:
    def __init__(self, celsius=0):
        self._celsius = celsius
    @property
    def fahrenheit(self):
        return self._celsius * 9 / 5 + 32
    @fahrenheit.setter
    def fahrenheit(self, value):
        self._celsius = (value - 32) * 5 / 9
t = Temperature(0)
print(t.fahrenheit)
t.fahrenheit = 212
print(t._celsius)
  1. A. 32.0 100.0
  2. B. 0.0 212.0
  3. C. 32.0 212.0
  4. D. 0.0 100.0

Correct answer: A. 32.0 100.0

0 Celsius converts to 32.0 Fahrenheit via the getter. Setting fahrenheit=212 invokes the setter, which converts back: (212-32)*5/9 = 100.0, stored in _celsius.

More Python Programming questions

Exams that ask this topic