How does Python handle mutable default arguments in function definitions?
- Mutable default arguments are automatically converted to immutable types.
- Python creates a new instance of the mutable object every time the function is called.
- Python raises an error when mutable objects are used as default arguments.
- The default mutable object is shared across all function calls.
Python handles mutable default arguments by sharing the same object across all function calls. This can lead to unexpected behavior if the mutable object is modified within the function. To avoid this, use immutable types as default arguments.
To flatten a list of lists, one might use a _______ within a list comprehension.
- Join() Function
- Nested List
- Nested Loop
- Zip() Function
To flatten a list of lists, you can use the 'zip()' function within a list comprehension. It combines elements from multiple lists into pairs, effectively flattening them.
What's the difference between global and nonlocal keywords when referring to variables in a function?
- 'global' is used to declare a variable as local to the current function.
- 'nonlocal' is used to declare a variable as accessible only within the current function.
- 'nonlocal' is used to declare a variable as global throughout the program.
- global' is used to declare a variable as accessible from any function in the program.
In Python, 'global' is used to declare a variable as accessible from any function in the program, while 'nonlocal' is used to declare a variable as accessible only within the current function or nested functions. These keywords help control variable scope.
If a member is named _memberName in a Python class, it is conventionally considered to be _______.
- Private
- Protected
- Public
- Static
If a member in a Python class is named with a single underscore prefix (e.g., _memberName), it is conventionally considered a private member.
If the base and derived classes both have a method named calculate, the derived class's method will _______ the base class's method when called from a derived class object.
- Call
- Extend
- Override
- Replace
If the base and derived classes both have a method named "calculate," the derived class's method will "Override" the base class's method when called from a derived class object.
The _______ attribute of an exception object returns a tuple containing an exception type, a value, and a traceback object.
- args
- error_details
- exception_info
- traceback_info
The 'args' attribute of an exception object returns a tuple containing information about the exception, including the exception type, a value (exception message), and additional details if any. This information is useful for debugging and error handling.
During data analysis, you are given a set of IDs from two different sources. You need to find IDs that are either in the first set or the second set but not in both. Which set operation will you apply?
- Difference
- Intersection
- Symmetric Difference
- Union
To find IDs that are in either the first set or the second set but not in both, you would apply the "Symmetric Difference" set operation. This will give you elements that are exclusive to each set.
How can you add multiple key-value pairs to an existing dictionary?
- Using a for loop to iterate over a list of tuples, where each tuple contains a key-value pair, and using the update() method to add them to the dictionary.
- By using the add() method and providing a list of key-value pairs.
- Using the extend() method to append a list of key-value pairs to the dictionary.
- By directly assigning a list of key-value pairs to the dictionary variable.
You can add multiple key-value pairs to an existing dictionary by using a for loop to iterate over a list of tuples, where each tuple contains a key-value pair, and then using the update() method to add them to the dictionary. The other options mentioned (add(), extend(), and direct assignment) are not standard methods for adding multiple key-value pairs to a dictionary.
In a series of if, elif, and else, how many times can elif appear?
- Any number
- Multiple times
- Never
- Only once
In Python, you can have multiple elif branches in a series of if, elif, and else statements. It allows you to test multiple conditions before falling back to the else block if none of the conditions are met.
To compile a Python script to bytecode without executing it, the command used is python -_______ .
- c
- compile
- noexec
- pyc
To compile a Python script to bytecode without executing it, the command used is python -m py_compile . The -m flag is used to invoke the py_compile module, and is the Python script you want to compile.
When creating a custom exception, it should typically inherit from _______.
- BaseException
- Error
- Exception
- RuntimeError
When creating a custom exception in Python, it is advisable to inherit from the 'Exception' class, which is the base class for all built-in exceptions. This allows your custom exception to be treated like a standard exception.
Which of the following will catch all exceptions in Python?
- try/except
- try/except: AllExceptions
- try/except: BaseException
- try/except: Exception
To catch all exceptions in Python, you can use 'try/except' with 'BaseException' as the exception type, which is the base class for all exceptions.