In a for-loop iterating through a list, if a specific condition is met and you want to skip the remaining code in the current iteration but not exit the loop, you would use the _______ statement.
- Break
- Continue
- Pass
- Skip
The 'continue' statement is used to skip the remaining code in the current iteration of a loop and move to the next iteration without exiting the loop. So, you would use 'Continue.'
The Python type(_______) would return .
- Text
- character
- str
- string
The Python 'type()' function checks the type of an object. To return '', you would use 'type(str)'.
What potential issue might arise from relying on the value of the name attribute in a module that is dynamically imported using functions like import()?
- Circular Import
- ImportError
- ModuleNotFoundError
- NameError
Relying on the name attribute in a dynamically imported module can lead to circular imports. Circular imports can create a situation where two or more modules depend on each other, causing potential issues and making the code harder to maintain.
What is the correct sequence of blocks in a conditional statement that uses all three components: if, elif, and else?
- if, elif, else
- if, else, elif
- else, if, elif
- elif, if, else
In Python, the correct sequence in a conditional statement that uses all three components is if, followed by one or more elif blocks, and finally, an optional else block.
A variable name in Python cannot start with a _______.
- Letter
- Number
- Special
- Underscore
In Python, variable names cannot start with a number, as they must begin with a letter or an underscore (_).
Sets in Python are _______ which means they cannot contain duplicate elements.
- immutable
- mutable
- ordered
- unhashable
Sets in Python are 'immutable,' which means they cannot contain duplicate elements. Once a set is created, you cannot change its elements, but you can add or remove elements from it.
What is the primary reason to use method overriding in object-oriented programming?
- To create a new method
- To extend a class's functionality
- To hide a method's implementation
- To improve code readability
The primary reason to use method overriding is to hide a method's implementation in the base class and provide a specialized implementation in the derived class. This allows for polymorphism and abstraction.
You're working with a dictionary where keys are country names and values are their capitals. You want to invert this dictionary. What potential issue might you encounter?
- No potential issue, dictionaries are always invertible.
- Potential issue: Overwriting keys if country names are not unique.
- Potential issue: Overwriting values if capitals are not unique.
- Potential issue: Python dictionaries cannot be inverted.
When inverting a dictionary, if the values (capitals) are not unique, you may encounter an issue where some values get overwritten. This results in the loss of information, as multiple countries may have the same capital. Inverting dictionaries assumes unique values for keys in the original dictionary.
You're reviewing a Python class and notice that many attributes are accessed and modified directly, without any checks. What could be introduced to provide controlled access and validation to these attributes?
- Creating a separate module to handle attribute access and modification.
- Defining the attributes within a function for validation.
- Encapsulating the attributes by making them private and providing getter and setter methods.
- Using global variables to track and validate attribute changes.
To provide controlled access and validation to attributes, you should encapsulate the attributes by making them private and providing getter and setter methods to enforce checks and validation.
Packages in Python provide a way of organizing related modules into a single _______ directory hierarchy.
- File System
- Folder
- Namespace
- Package
Packages in Python help organize related modules into a single folder hierarchy, making it easier to manage and access them in a structured way.
Which Python method is executed when the context of a with statement is entered?
- __enter__()
- __exit__()
- __init__()
- __open__()
The __enter__() method is executed when the context of a 'with' statement is entered. It typically sets up the required resources or context for the code block within the 'with' statement.
Given a scenario where a system should notify a user if the storage space goes below 5% or above 95%. Which of the following combinations of operators will be suitable for the condition?
- if storage < 5 or storage > 95:
- if storage <= 5 or storage >= 95:
- if storage > 5 and storage < 95:
- if 5 <= storage <= 95:
To check if the storage space goes below 5% or above 95%, you should use the condition if storage <= 5 or storage >= 95:. This condition covers both cases, ensuring that the system notifies the user when the storage falls below 5% or exceeds 95%. The other options do not provide the correct combination of operators for this scenario.