To iterate over a list in reverse order using a for loop, one can use the _______ method of the list.
- append()
- extend()
- reverse()
- sort()
To iterate over a list in reverse order using a for loop, you can use the reverse() method, which reverses the elements in the list in place, allowing you to traverse it in reverse order.
Which of the following is a default method executed when a new instance of a class is created?
- create()
- init()
- main()
- new()
The 'init()' method, also known as the constructor, is executed automatically when a new instance of a class is created. It initializes object attributes.
In the context of Python packages, what is the significance of a directory containing an init.py file?
- It indicates a directory is empty and serves no purpose.
- It is only used for version control purposes.
- It is used to exclude the directory from package scanning.
- It marks the directory as a special package directory.
In Python, the presence of an __init__.py file in a directory signifies that the directory should be treated as a Python package. It allows you to organize related modules into a package, and it also enables the use of relative imports within the package. Without this file, Python won't recognize the directory as a package.
In Python, the operator == checks for _______.
- Assignment
- Identity
- Inequality
- Value equality
The operator '==' checks for value equality. It returns True if the values on both sides of the operator are equal.
When a method is decorated with @staticmethod, it cannot access or modify _______ specific data.
- class
- global
- instance
- local
When a method is decorated with '@staticmethod', it cannot access or modify 'instance' specific data. '@staticmethod' methods are bound to the class and cannot access or modify instance-specific attributes or methods.
You are asked to ensure that certain attributes in a Car class should never be overridden by any child class. Which mechanism would you use to ensure this?
- @final decorator (custom)
- @property decorator
- method overriding
- private attributes
To ensure that certain attributes in a Car class should never be overridden by any child class, you can use private attributes. By convention, prefix attribute names with an underscore (e.g., _attribute) to indicate that they should not be accessed or overridden externally.
If a context manager's enter method raises an exception, what happens to its exit method?
- It depends on Python's version.
- It's called after enter regardless of exceptions.
- It's called only if enter completes without exceptions.
- It's not called at all.
If the enter method raises an exception, the exit method is not called at all, ensuring resource cleanup doesn't happen in case of an error during setup.
Which method in a class is responsible for deleting an attribute?
- del()
- delete()
- destroy()
- remove()
The 'del()' method is used to delete an attribute or perform any necessary cleanup when an instance of a class is about to be destroyed.
Which feature of Python allows us to handle different numbers and types of arguments?
- Default arguments
- Function overloading
- Operator overloading
- Variable-length arguments
Variable-length arguments (e.g., *args and **kwargs) allow Python functions to accept a varying number of arguments, enabling flexibility in function calls.
What keyword is used in Python to make a decision?
- choice
- decide
- else
- if
In Python, the 'if' keyword is used to make a decision. It allows you to execute a block of code conditionally based on the evaluation of a given expression.