Bird
Raised Fist0

Consider this class:

hard🚀 Application Q9 of Q15
Python - Classes and Object Lifecycle
Consider this class:
class Circle:
    pi = 3.14
    def __init__(self, radius):
        self.radius = radius
    def circumference(self):
        return 2 * Circle.pi * self.radius

c = Circle(5)
print(c.circumference())

What is the output and why?
A31.400000000000002 because pi is a class variable used in calculation
BError because pi should be accessed with self.pi
C10 because radius is multiplied by 2 only
DNone because circumference method does not return
Step-by-Step Solution
Solution:
  1. Step 1: Understand class variable usage

    pi is a class variable accessed as Circle.pi, which is valid.
  2. Step 2: Calculate circumference

    Formula: 2 * 3.14 * 5 = 31.4 (floating point may show slight difference).
  3. Final Answer:

    31.400000000000002 because pi is a class variable used in calculation -> Option A
  4. Quick Check:

    Class variables accessed via ClassName [OK]
Quick Trick: Access class vars with ClassName.var or self.var [OK]
Common Mistakes:
MISTAKES
  • Expecting error accessing class variable
  • Ignoring floating point precision
  • Forgetting to return value in method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes