Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q5 of 15
Python - Encapsulation and Data Protection
What will be the output of this code?
class Temperature:
    def __init__(self, celsius):
        self._celsius = celsius

    @property
    def fahrenheit(self):
        return (self._celsius * 9/5) + 32

t = Temperature(0)
print(t.fahrenheit)
AError: AttributeError
B32.0
C0
Dnull
Step-by-Step Solution
Solution:
  1. Step 1: Understand property without setter

    fahrenheit is a read-only property calculated from _celsius.
  2. Step 2: Calculate output for celsius=0

    fahrenheit = (0 * 9/5) + 32 = 32.0
  3. Final Answer:

    32.0 -> Option B
  4. Quick Check:

    Read-only property returns calculated value [OK]
Quick Trick: Property can compute values without setter [OK]
Common Mistakes:
  • Expecting 0 instead of converted value
  • Assuming error due to missing setter
  • Thinking property returns null

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes