Classes & Objects · Question 13 · Medium
fahrenheit is declared with @property and is accessed without brackets. What does this code print?
class Temp:
def __init__(self, c):
self.c = c
@property
def fahrenheit(self):
return self.c * 9 / 5 + 32
t = Temp(100)
print(t.fahrenheit)
- 212.0
- 212
- TypeError
- AttributeError
Answer and explanation
Answer: A. 212.0
Explanation: @property lets a method be read like an attribute, so t.fahrenheit runs the method. 100 × 9 / 5 is 180.0 because / gives a float, and adding 32 gives 212.0.
Previous: A class method reads a counter that __init__ increases on the class. What is… · Next: What is the key difference between a method marked @classmethod and one marked…
Practise classes & objects
Try a fresh set of classes & objects questions with an explanation after every answer, free and without signing up: Classes & Objects practice. Or sit a full placement mock test.
More classes & objects questions
- Three classes in a chain each extend fee() using super(). What does the last…
- The Point class defines __eq__. What do == and is give for two separately…
- A class method reads a counter that __init__ increases on the class. What is…
- What is the key difference between a method marked @classmethod and one marked…
- B is a child of A. What do isinstance, a direct type comparison and issubclass…
- D inherits from both B and C, and B and C both inherit from A. Which who()…
- Every class in this diamond calls super().go(). What string does D().go() build?
- The Coin class defines both __str__ and __repr__. What is printed for the…
- total is a property with only a getter. What happens when the code tries to…
- sell() uses self.stock -= 1 where stock starts as a class attribute, and the…
More python practice
Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators · Python Strings · Lists & Tuples · Dictionaries & Sets · Conditions & Loops · Functions & Scope
LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge