Advanced Python Quiz Questions
Advanced Python Quiz Questions
The `__init__` method in a Python class, often referred to as the constructor, is executed upon the creation of a new class instance. It initializes class attributes and sets up the initial state of the object. This method is automatically called when an instance is created and allows for any necessary parameters to customize the instance's initial configuration.
The `len()` function, when used with dictionaries in Python, returns the number of key-value pairs in the dictionary. It provides a count of how many elements (pairs) are contained in the dictionary. This is useful for quickly determining the size of a dictionary.
The `yield` keyword is used in Python to define a generator function, which returns an iterator. Unlike `return`, which exits the function completely, `yield` pauses the function saving its state, and resumes where it left off when called again. This allows the function to produce a series of values over time rather than computing them all at once and sending them back. This makes `yield` more memory efficient than `return` when dealing with large datasets.
Two variables point to the same list in Python when one is assigned to the other without copying, meaning they reference the same memory location. Modifications to the list via one variable will reflect in the other. For example: ```python a = [1, 2, 3] b = a a.append(4) ``` Here, both a and b refer to the same list object, so appending 4 to a also modifies b, resulting in [1, 2, 3, 4]
Decorators in Python are a form of metaprogramming that can modify or wrap a function or class to change or enhance its behavior without altering its source code. Common use cases include logging, enforcing access control, imposing restrictions, instrumentation, and memoization. Decorators provide a convenient, readable way to add functionality to existing code.
Python uses `try` and `except` blocks to handle exceptions, allowing for clean exception management by catching errors and continuing program execution. Unlike Java's `try` and `catch`, Python omits the need for exception declarations or checked exceptions, providing flexibility but requiring careful management to avoid runtime errors.
The printed result of the operation is `{3}`, which is the intersection of sets `a` and `b`. The `&` operator calculates the intersection, resulting in a set containing only elements that are present in both `a` and `b`. In this case, the number 3 is the only common element.
The output of the code snippet is [1], [1, 1]. This occurs because the default value for the parameter x in the function foo is a mutable list. When foo() is called for the first time, the list [1] is returned and stored as the default value. On subsequent calls, this same list is used and modified, resulting in the list containing [1, 1] on the second call.
In Python, the `bool()` function returns `True` for any non-empty string, regardless of its content. Since 'False' is a non-empty string, it evaluates to `True` when converted to a boolean with `bool()`. It's important to note that `bool()` does not consider the content of the string but only its emptiness.
The `finally` block in Python is designed to always execute after the `try` block, even if a return statement is hit within `try`. In the code snippet, although `try` specifies `return 1`, the `finally` block overrides it with `return 2`. `finally` ensures clean-up code runs, hence why it commits the function to return 2, not 1.