You have two sets, one containing all employees who attended a training session and another with employees who completed an online assessment. You want to find out who attended the training but did not complete the assessment. Which set operation would help?
- Difference
- Intersection
- Symmetric Difference
- Union
To find employees who attended training but did not complete the assessment, you would use the "Difference" set operation. This will give you the elements that are in the first set but not in the second.
In which scenario might you use the built-in import() function instead of a regular import statement?
- When you want to import a module as a built-in Python module.
- When you want to import a module dynamically based on user input or conditions.
- When you want to import a module from an external directory.
- When you want to improve code readability by avoiding long import statements.
The 'import()' function is used to import modules dynamically at runtime, often when the module names are determined by user input or other dynamic factors.
What is the primary purpose of the with statement in Python?
- Create a loop construct
- Define a function context
- Import external modules
- Simplify exception handling
The primary purpose of the 'with' statement in Python is to simplify exception handling by ensuring proper resource management and cleanup. It's often used with context managers to guarantee resource release.
You've created a module mymodule.py, and inside it, there's a function named myfunc. However, when another developer tries to import myfunc using from mymodule import myfunc, they get an error that it doesn't exist. What could be the possible reasons?
- There is a typo in the function name or module name.
- The module mymodule.py is not in the same directory as the Python script that's trying to import it.
- The function myfunc is defined inside a class in mymodule.py, and it should be accessed as from mymodule import MyClass.
- The function myfunc is marked as private (starts with an underscore) and needs to be imported using _from mymodule import _myfunc_.
The error could occur because the module mymodule.py is not in the same directory as the script trying to import it. To resolve this, you can ensure that mymodule.py is in a directory listed in Python's sys.path or use relative imports if they are organized in packages.
You notice a script is taking more memory than expected even after finishing its tasks. You suspect some objects aren't being garbage collected. How can you explicitly attempt to clean them up?
- clear_memory()
- del objects
- gc.collect()
- mem_cleanup(objects)
To explicitly attempt to clean up objects and free memory, you can use gc.collect(). This function is part of Python's gc (garbage collection) module and triggers the garbage collection process, which reclaims memory occupied by unreachable objects.
In a custom Fraction class, if you want to compare two fractions using the == operator, which method should be overloaded?
- Overload the addition operator (+) in the Fraction class.
- Overload the division operator (/) in the Fraction class.
- Overload the equality operator (==) in the Fraction class.
- Overload the multiplication operator (*) in the Fraction class.
To compare two fractions using the == operator, you should overload the equality operator (==) in the Fraction class to define how fractions should be compared for equality.
How do you access the first item in a tuple named my_tuple?
- my_tuple(0)
- my_tuple<0>
- my_tuple[0]
- my_tuple{0}
To access the first item in a tuple, you use square brackets and specify the index. For example, my_tuple[0] retrieves the first item.
When trying to execute an expression like obj1 * obj2 for custom objects, Python internally calls _______.
- __mul__
- __mult__
- __multiply__
- __product__
When you attempt to execute an expression like obj1 * obj2 for custom objects, Python internally calls the __mul__ method. Implementing this method allows you to define how the multiplication operation behaves for your objects.
How can you remove an element from a set without raising an error if the element doesn't exist?
- delete()
- discard()
- pop()
- remove()
You can use the discard() method to remove an element from a set without raising an error if the element doesn't exist in the set. It's a safe removal method.
How does the method resolution order (MRO) in Python impact method overloading in the context of inheritance?
- It allows Python to call overloaded methods based on the order they are defined in the class.
- It doesn't impact method overloading; Python uses the last defined method with the same name.
- MRO affects method overloading by considering the class hierarchy and the order of base classes.
- MRO doesn't matter; Python uses the method with the same name in the most derived class.
The MRO in Python determines the order in which methods are searched for in class hierarchies. When methods are overloaded, the MRO helps Python find the appropriate method by considering the class hierarchy and the order of base classes. The method is then called based on the MRO.