In which type of loop structures can the break statement be used in Python?
- both for and while loops
- for loop
- neither for nor while loops
- while loop
The 'break' statement can be used in both 'for' and 'while' loops in Python. It is used to exit the loop prematurely based on a certain condition, making it a useful control structure for loop termination.
Which method is used to remove a key-value pair from a dictionary by specifying the key?
- delete()
- pop()
- popitem()
- remove()
The pop() method is used to remove a key-value pair from a dictionary by specifying the key. It returns the value associated with the key and removes the key-value pair from the dictionary. The del statement can also be used to delete a key-value pair by specifying the key. The popitem() method removes and returns an arbitrary key-value pair, while remove() is not a standard dictionary method.
The _______ method in a context manager returns the resource that will be managed.
- enter()
- exit()
- init()
- resource()
The 'enter()' method in a context manager returns the resource that will be managed. It is called when entering the 'with' block.
Which built-in Python function can be used to determine if a class is a subclass of another?
- inheritsfrom()
- instanceof()
- issubclass()
- subclassof()
You can use the 'issubclass()' built-in function in Python to determine if a class is a subclass of another. This function takes two arguments: the derived class and the base class. If the derived class is indeed a subclass of the base class, it returns True; otherwise, it returns False. This function is commonly used for checking class hierarchies and relationships.
You are designing a Vector class to represent 2D vectors, and you want to add two vectors using the + operator. How would you implement this?
- Create a new class, VectorAdder, to handle vector addition.
- Implement a separate function, add_vectors(), for adding vectors.
- Overload the + operator in the Vector class, so it combines the x and y components of the vectors.
- Overload the += operator for the Vector class to support in-place addition of vectors.
The correct approach is to overload the + operator in the Vector class, allowing for a natural syntax for vector addition. This is a common operator overloading scenario.
The process of converting source code into bytecode, which is then executed by the Python interpreter, is called _______.
- Compilation
- Compilation
- Execution
- Interpretation
Python uses interpretation, not compilation, to execute code. The process is called interpretation, not compilation.
Which keyword is used in Python to create a derived class?
- baseclass
- class
- extends
- superclass
In Python, the 'class' keyword is used to create both base (parent) classes and derived (child) classes. The derived class is created by inheriting from a base class.
The _______ file can control which modules are imported when 'from package import *' is invoked.
- import_rules.txt
- init.py
- main.py
- package_config.ini
The init.py file in a Python package can control which modules are imported when 'from package import *' is used. It allows you to define the package's public interface.
Which of the following statements is true about private attributes in a Python class?
- Private attributes are accessible and modifiable outside the class, just like public attributes.
- Private attributes are denoted by a double underscore prefix, such as "__private". They can be accessed and modified outside the class.
- Private attributes are denoted by a single underscore prefix, such as "_private". They are intended for internal use within the class.
- Private attributes are inaccessible from outside the class, making them more secure.
Private attributes in Python are typically denoted by a single underscore prefix (e.g., "_private"). While they can still be accessed from outside the class, the convention is to treat them as non-public and for internal use.
You are writing a nested function, and you want to modify a variable from the outer function. However, you don't want this change to affect the global scope. How can you achieve this?
- Use the global keyword
- Use the local keyword
- Use the nonlocal keyword
- Use the private keyword
To modify a variable from an outer function without affecting the global scope, use the 'nonlocal' keyword. It allows you to access and modify the variable in the closest outer scope.
Which of the following is a recommended use case for custom context managers?
- Basic arithmetic calculations.
- Closing a file after reading/writing data
- Sorting a list of integers.
- Web scraping with BeautifulSoup.
A recommended use case for custom context managers is closing a file after reading or writing data. This ensures proper resource cleanup and prevents resource leaks when dealing with files.
The variables that are defined inside a function are referred to as _______ variables.
- Global
- Instance
- Local
- Non-local
Variables defined inside a function are known as 'local' variables. They are only accessible within that function's scope.