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

Question 27

Consider the following Python code:



Which of the following statements is true?

  • A

    An instance of MyClass has an attribute named custom_attr.

  • B

    MyClass.custom_attr returns None.

  • C

    MyClass.custom_attr returns 42.

  • D

    MyClass.custom_attr raises an AttributeError.

Reveal correct answer

Correct answer: C

A.

While MyClass has a class attribute named custom_attr, instances of MyClass do not automatically have an instance attribute named custom_attr. If you create an instance of MyClass and try to access custom_attr, it will return the class attribute custom_attr, unless an instance attribute custom_attr has been set.

B.

MyClass.custom_attr does not return None. The metaclass Meta adds a class attribute named custom_attr with the value 42 during the creation of MyClass.

C.

The metaclass Meta adds a class attribute named custom_attr with the value 42 during the creation of MyClass. Therefore, MyClass.custom_attr returns 42.

D.

MyClass.custom_attr does not raise an AttributeError. The metaclass Meta adds a class attribute named custom_attr during the creation of MyClass.

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