PCPP 32 10x PCPP Certified Professional Python Programmer 1 · Free Practice Question Hard

Question 6

Consider the following Python code snippet:



What will be the output of print(MyClass.instances)?

  • A

    3

  • B

    1

  • C

    0

  • D

    An AttributeError will be raised.

Reveal correct answer

Correct answer: A

A.

The instances attribute is incremented each time an instance of MyClass is created. Since MyClass is instantiated three times, MyClass.instances will be 3.

B.

While MyClass.instances would be 1 after the first instance of MyClass is created, it will be incremented two more times when the second and third instances are created, so the final value will be 3, not 1.

C.

The instances attribute is incremented each time an instance of MyClass is created. Since MyClass is instantiated three times, MyClass.instances will be 3, not 0.

D.

The instances attribute is created in the Meta metaclass and assigned to the class during its creation, so accessing MyClass.instances will not raise an AttributeError.

Discussion

Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need