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 (_).
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.
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 purpose of the import keyword in Python?
- To create custom functions
- To declare class names
- To define variables
- To include external modules
The 'import' keyword in Python is used to include external modules or libraries, making their functionality available in your script.
To loop through two lists simultaneously, one can use the _______ function in conjunction with a for loop.
- concat()
- enumerate()
- itertools.product()
- zip()
To iterate through two lists simultaneously, the 'zip()' function is used in conjunction with a for loop. 'zip()' combines elements from two or more iterables into tuples, allowing you to iterate over pairs of elements. This is a common technique for parallel iteration through multiple lists.
A colleague is facing an ImportError when trying to import a package. Upon checking, you notice the package directory lacks a certain file, making Python not recognize it as a package. Which file is missing?
- init.py
- main.py
- package.py
- setup.py
The 'init.py' file is missing. This file is required in Python package directories for package recognition and proper importing.
Which operator will return True if both the operands are true?
- and
- not
- or
- xor
The 'and' operator in Python returns True if both of its operands are true. It performs a logical AND operation. For example, True and True evaluates to True, while True and False evaluates to False.
When dealing with file I/O, the _______ statement is often used to ensure that the file is properly closed after its suite finishes.
- close
- open
- try-finally
- with
When dealing with file I/O, the try-finally statement is often used to ensure that the file is properly closed after its suite finishes, even if an exception is raised.
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.
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.