PCPP Python Decorators and Metaprogramming Quiz

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Thames
T
Thames
Community Contributor
Quizzes Created: 11982 | Total Attempts: 187,298
| Questions: 20 | Updated: Aug 15, 2026
Please wait...
Question 1 / 21
🏆 Rank #-- ▾
0 %
0/100
Score 0/100

1. Properties in Python are implemented using the descriptor ______.

Explanation

In Python, properties are implemented through the descriptor protocol. A property object defines methods such as `__get__` and `__set__`, allowing it to control how an attribute is read or assigned. This lets a class provide getter and setter behavior while users access the value using ordinary attribute syntax.

Submit
Please wait...
About This Quiz
PCPP Python Decorators and Metaprogramming Quiz - Quiz

This quiz evaluates your mastery of Python decorators and metaprogramming techniques covered in the PCPP curriculum. You'll test your understanding of function decorators, class decorators, metaclasses, descriptors, and advanced metaprogramming patterns. Essential for developers preparing for PCPP certification and those building sophisticated Python applications.

2.

What first name or nickname would you like us to use?

You may optionally provide this to label your report, leaderboard, or certificate.

2. In Python, the __call__ method makes an instance of a class behave like a ______.

Explanation

Defining `__call__` lets you invoke an instance using parentheses, just like a function. When called, Python runs the instance’s `__call__` method, which can accept arguments and return a result.

Submit

3. What does @staticmethod indicate about a method?

Explanation

A `@staticmethod` is associated with a class but does not receive an implicit first argument. Unlike an instance method, it gets no `self`; unlike a class method, it gets no `cls`. It can be called through either the class or an instance.

Submit

4. The @classmethod decorator modifies a method to receive the ______ as its first argument instead of an instance.

Explanation

A method decorated with `@classmethod` is bound to the class rather than a particular object. Python automatically passes the class—conventionally named `cls`—as its first argument, allowing the method to access or modify class-level data.

Submit

5. Which of the following is a valid use case for metaprogramming?

Explanation

Metaprogramming lets a program create or modify code at runtime or compile time. An ORM can use it to dynamically generate model classes from database schemas, reducing repetitive code and keeping models aligned with the underlying data structure.

Submit

6. A decorator factory is a function that returns a ______ which then acts as a decorator.

Explanation

A decorator factory lets you configure a decorator before applying it. It returns a decorator function, which is then used to wrap or modify the behavior of another function or class.

Submit

7. In metaprogramming, what does the __prepare__ method in a metaclass do?

Explanation

`__prepare__` is called by a metaclass before the class body runs. It returns the namespace in which that body executes, allowing the metaclass to customize how class attributes are collected—for example, using an ordered or specialized mapping.

Submit

8. Which special method allows you to intercept attribute access that would normally raise AttributeError?

Explanation

Python calls `__getattr__` when normal attribute lookup fails, allowing a class to provide a fallback value or handle the missing attribute. By contrast, `__getattribute__` runs for every attribute access, while `__getitem__` handles indexed access and `__get__` supports descriptor behavior.

Submit

9. The __getattribute__ method is called ______ an attribute is accessed on an object.

Explanation

In Python, `__getattribute__` runs whenever an attribute lookup is performed on an object. It can customize or intercept the lookup; if the attribute isn’t found, Python may then call `__getattr__` as a fallback.

Submit

10. What is the difference between a data descriptor and a non-data descriptor?

Explanation

In Python, a data descriptor defines `__set__` or `__delete__` (often alongside `__get__`), while a non-data descriptor does not. This distinction affects attribute lookup: data descriptors take precedence over an instance’s attribute dictionary, whereas non-data descriptors can be overridden by an attribute stored on the instance.

Submit

11. Which built-in function is used to apply a decorator to a function in Python?

Explanation

Python applies a decorator using the `@decorator` syntax placed directly above a function definition. This shorthand is equivalent to passing the function to the decorator and assigning the returned function back to the original name.

Submit

12. What is a descriptor in Python?

Explanation

A descriptor is an object that customizes how an attribute is accessed, assigned, or deleted. Python recognizes descriptors through their `__get__`, `__set__`, or `__delete__` methods, which let them control attribute behavior in classes.

Submit

13. Which method of a metaclass is called when a new class is created?

Explanation

When Python creates a class, it calls the metaclass’s `__new__` method to build and return the class object. The metaclass’s `__init__` may then initialize that object. `__call__` is typically involved when creating instances, while `__create__` is not a standard Python metaclass method.

Submit

14. The default metaclass for all Python classes is ______.

Explanation

In Python, a metaclass is the class that creates and defines the behavior of other classes. When no metaclass is explicitly specified, Python uses `type` by default. Ordinary class statements are processed through `type`, which creates the class object; instances of that class are then created according to its definition.

Submit

15. In metaprogramming, what is a metaclass?

Explanation

A metaclass defines how classes are created and behave. Just as ordinary classes create instances, metaclasses create classes, making classes themselves their instances. In Python, for example, `type` is the default metaclass.

Submit

16. A class decorator can modify the behavior of an entire class by wrapping its ____ method.

Explanation

A class decorator receives the class object and can replace or wrap its `__init__` method. This lets it change how every instance is initialized—for example, by adding setup logic or modifying constructor arguments—without changing the class’s original definition.

Submit

17. What does the functools.wraps decorator do when applied to a wrapper function?

Explanation

`functools.wraps` copies key metadata from the wrapped function—such as its name and docstring—to the wrapper. This keeps the wrapped function identifiable and makes documentation and introspection tools display its original details.

Submit

18. Which of the following correctly implements a simple decorator that counts function calls?

Explanation

The wrapper intercepts each call, increments its call counter, forwards the original arguments to `f`, and returns the result. However, as written, `wrapper.count` is never initialized, so the first call raises an `AttributeError`. Set `wrapper.count = 0` before returning the wrapper for the decorator to work.

Submit

19. In Python, a decorator is fundamentally a ______ that takes a function as input and returns a modified function.

Explanation

A decorator is a function that accepts another function as an argument, adds or changes behavior, and returns a function to use in its place. This lets you extend a function’s behavior without modifying its original code.

Submit

20. What is the primary purpose of a decorator in Python?

Explanation

A decorator wraps a function or class to add or alter behavior while leaving the original source code unchanged. This makes it easy to reuse features such as logging, authentication, or timing across multiple functions.

Submit
×
Saved
Thank you for your feedback!
View My Results
Cancel
  • All
    All (20)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
Properties in Python are implemented using the descriptor ______.
In Python, the __call__ method makes an instance of a class behave...
What does @staticmethod indicate about a method?
The @classmethod decorator modifies a method to receive the ______ as...
Which of the following is a valid use case for metaprogramming?
A decorator factory is a function that returns a ______ which then...
In metaprogramming, what does the __prepare__ method in a metaclass...
Which special method allows you to intercept attribute access that...
The __getattribute__ method is called ______ an attribute is accessed...
What is the difference between a data descriptor and a non-data...
Which built-in function is used to apply a decorator to a function in...
What is a descriptor in Python?
Which method of a metaclass is called when a new class is created?
The default metaclass for all Python classes is ______.
In metaprogramming, what is a metaclass?
A class decorator can modify the behavior of an entire class by...
What does the functools.wraps decorator do when applied to a wrapper...
Which of the following correctly implements a simple decorator that...
In Python, a decorator is fundamentally a ______ that takes a function...
What is the primary purpose of a decorator in Python?
play-Mute sad happy unanswered_answer up-hover down-hover success oval cancel Check box square blue
Alert!