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.
In Python, how can you execute a block of code multiple times without using a loop?
- List Comprehension
- Recursion
- exec() function
- goto statement
In Python, you can execute a block of code multiple times without using a loop by using recursion, where a function calls itself. This allows for repeated execution without an explicit loop construct.
A Python script unexpectedly terminates, and upon investigation, you discover a circular import. What could be a probable solution?
- Enable Circular Imports
- Reorder the Import Statements
- Use the __init__.py File in Packages
- Use the sys.modules Dictionary
Circular imports occur when two or more modules import each other. To solve this, reorder the import statements so that they don't create a circular dependency. You may also use the __init__.py file in packages to control imports.
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.