Which control structure allows the checking of multiple expressions for truth value and executing a block of code as soon as one of the conditions evaluates to true?
- for loop
- if-elif-else
- switch-case
- while loop
The if-elif-else control structure allows the checking of multiple expressions for truth value. It executes the block of code associated with the first true condition and then exits the structure.
Which loop is most appropriate when the number of iterations is known beforehand?
- do-while
- for
- foreach
- while
The 'for' loop is most appropriate when the number of iterations is known beforehand. It is often used to iterate over a sequence or a range of values.
What does the as keyword do in the context of importing modules in Python?
- Creates an alias for the module
- Renames the imported function
- Renames the module
- Specifies the module's version
The as keyword creates an alias for the imported module, allowing you to use a different name when referencing it in your code. This can be useful for avoiding naming conflicts.
To get a deep copy of nested lists, the copy module provides the _______ method.
- copy.deep()
- copy.deep_copy()
- copy.deepcopy()
- copy.nested_copy()
To create a deep copy of nested lists in Python, you can use the copy.deepcopy() method from the copy module. It ensures that nested structures are fully duplicated, avoiding any shared references.
If a class has the slots attribute defined, it means the instances of that class will not have a _______ dictionary.
- attributes
- dict
- namespace
- slots
If a class has the slots attribute defined, it means the instances of that class will not have a 'dict' dictionary. The 'slots' attribute restricts the instance to only store attributes explicitly listed in 'slots'.
The _______ file can be used to execute initialization code for a package.
- init.init
- init.py
- init.txt
- package.py
The __init__.py file is used to execute initialization code for a package. It runs when the package is imported or used, allowing you to set up package-level configurations or variables.
Which special variable in Python represents the name of the current module?
- __current__
- __main__
- __module__
- __name__
The special variable __name__ represents the name of the current module in Python. When a Python script is run, the value of __name__ is set to '__main__', indicating that it's the main script being executed. In imported modules, __name__ is set to the module's name. It's often used to check if a script is run as the main program or imported as a module.
If you want to make a variable defined in an outer function accessible to an inner function, you use the _______ keyword.
- global
- inner
- outer
- public
To make a variable defined in an outer function accessible to an inner function, you use the 'global' keyword. It indicates that the variable should be treated as a global variable.
If you wish to make a shallow copy of a list, you can use the _______ method.
- clone() Method
- copy() Method
- deepcopy() Method
- slice() Method
To make a shallow copy of a list, you can use the 'copy()' method. It creates a new list containing references to the same objects as the original list.
What does the finally block represent in Python's exception handling?
- A block for catching exceptions
- A block for cleaning up resources
- A block for defining custom exceptions
- A block for handling exceptions
The 'finally' block is used for cleaning up resources, such as closing files or network connections, regardless of whether an exception was raised or not.
Which of the following loop types executes at least once, irrespective of the test condition?
- do-while loop
- for loop
- until loop
- while loop
A do-while loop in Python executes at least once, irrespective of the test condition, as the condition is checked after the loop body.
In Python, the _______ method is used to overload the + operator.
- __add__
- __init__
- __main__
- __str__
In Python, the __add__ method is used to overload the + operator for user-defined classes. This method is called when the "+" operator is applied to objects of the class.