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)- A. 32.0 100.0
- B. 0.0 212.0
- C. 32.0 212.0
- 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
- What is printed? python def f(a, b, , c=10, kwargs): return a + b + c + sum(kwargs.values()) print(f(1, 2, d=3, e=4)) print(f(1, 2, c=100))
- What is printed? python class Animal: sound = "..." def makesound(self): return f"{type(self).name} says {self.sound}" class Dog(Animal)…
- What is printed? python def outer(): x = [] def appendVal(v): x.append(v) return x return appendVal f1 = outer() f2 = outer() print(f1(1)…
- What is printed? python def validate(f): def wrapper(x): if x < 0: raise ValueError("negative") return f(x) return wrapper @validate def…
- What is printed? python def makeFuncs(): return [lambda x, n=n: x n for n in range(1, 4)] fns = makeFuncs() print([f(2) for f in fns])
- What is printed? python def f(n): if n == 0: return 0 return 1 + f(n - 1) print(f(100))