PythonFan.org
Toggle Menu
Home
Online Python Compiler
Tutorials
Python FastAPI
Python Pandas
Python PyTorch
Python Seaborn
Blog
All Posts
Advanced Python Techniques
Challenge yourself with decorators,generators,and context managers.
1. What is the primary purpose of a decorator in Python?
To create new classes dynamically
To modify or extend the behavior of a function or method
To handle exceptions in nested code blocks
To define static methods in a class
2. Which of the following are valid ways to implement a context manager in Python?
Defining __enter__ and __exit__ methods in a class
Using the @contextmanager decorator from the contextlib module
Directly using the 'with' keyword without additional setup
Subclassing the built-in ContextManager base class
3. A generator function returns a generator iterator when called.
True
False
4. What does the acronym 'PEP' stand for in Python?
5. What is a metaclass in Python?
A class that defines the behavior of other classes
A function that dynamically creates class instances
A module containing utility classes
A subclass of a built-in immutable type
6. Which of the following statements about asyncio in Python are true?
It is used for writing concurrent code using async/await syntax
It relies on multi-threading for parallel execution by default
Coroutines are defined using the 'async def' keyword
The 'await' keyword can be used inside any regular function
7. Defining __slots__ in a class reduces memory usage by preventing dynamic attribute creation.
True
False
8. What is the term for an anonymous generator expression enclosed in parentheses (e.g., (x for x in range(10)))?
9. Which method must a descriptor implement to be classified as a 'data descriptor'?
__get__
__set__
__delete__
__call__
10. Which of these are examples of higher-order functions in Python's standard library?
map()
filter()
sorted()
lambda
11. Calling an async function directly (like a regular function) will execute its code immediately.
True
False
12. What is the default metaclass used to create all user-defined classes in Python?
13. What method is invoked when exiting the 'with' block of a context manager?
__exit__
__close__
__leave__
__done__
14. Which techniques help optimize memory usage for large datasets in Python?
Using __slots__ in classes to limit dynamic attributes
Using generator expressions instead of list comprehensions
Disabling the garbage collector with gc.disable()
Using sys.getsizeof() to monitor object sizes
15. Which function is recommended to run the top-level coroutine in an asyncio application?
asyncio.run()
asyncio.start()
asyncio.execute()
asyncio.launch()
Reset
Answered 0 of 0 — 0 correct