In what scenario is the __name__ attribute of a module set to its own module name instead of "__main__"?
- When the module is a built-in module
- When the module is defined in Python 2.x
- When the module is imported as a library
- When the module is the main program
The __name__ attribute of a module is set to its own module name when it's imported as a library by another module. It's set to "__main__" when the module is the main program being executed.
Which of the following tools is used to create isolated Python environments?
- Anaconda
- PyDist
- PyEnv
- Virtualenv
'Virtualenv' is a tool used to create isolated Python environments. It allows you to work on different projects with their own dependencies without conflicts.
The process of giving an extended meaning to operators for user-defined data types is called _______.
- Operator Customization
- Operator Definition
- Operator Extension
- Operator Overloading
The process of giving an extended meaning to operators for user-defined data types is called Operator Overloading. It allows you to define how operators behave with custom objects.
In what scenario might using a defaultdict be more appropriate than using a standard dictionary?
- When you need constant-time lookups
- When you need to handle missing keys more gracefully
- When you need to sort the dictionary
- When you want to save memory space
A defaultdict is useful when you want to handle missing keys more gracefully by providing a default value or factory function. It can simplify code that deals with non-existent keys.
In Python, which prefix is conventionally used to denote a private member in a class?
- __private
- _private
- priv
- private
In Python, a private member in a class is conventionally denoted using a single underscore prefix, like _private. While not enforced by the language, it's a widely followed naming convention to indicate that the member should not be accessed directly from outside the class.
How does the use of custom exceptions benefit a large-scale software project?
- Custom exceptions help
- Custom exceptions make code cleaner
- Custom exceptions provide
- Custom exceptions simplify debugging
Using custom exceptions simplifies debugging by allowing you to define specific error types with meaningful names and error messages, making it easier to identify issues. It doesn't inherently make code cleaner but adds clarity and maintainability.
After installing a package using pip, a colleague isn't able to import it in their script. What could be a probable reason?
- They are using a different Python interpreter
- The package is not properly installed
- The package is not in their system PATH
- There's a compatibility issue with the package
If a colleague is using a different Python interpreter than the one where the package was installed, they won't be able to import it. This often happens when multiple Python installations exist on the system. It's important to ensure that everyone is using the same Python interpreter or virtual environment. The other options could be potential issues but are not directly related to the colleague's problem.
Which statement is used in a loop to terminate it and transfer the execution to the next statement following the loop?
- break
- continue
- exit
- return
The 'break' statement is used in a loop to terminate it prematurely and transfer the execution to the next statement following the loop. It is often used to exit a loop based on a certain condition.
What is Python's primary scripting paradigm?
- Functional
- Imperative
- Object-oriented
- Procedural
Python's primary scripting paradigm is object-oriented. However, it supports multiple paradigms like procedural and functional too.
The _______ block is executed no matter if the try block raises an error or not.
- else
- except
- finally
- raise
The finally block is executed regardless of whether an exception is raised in the try block or not. It is often used for cleanup operations.
If a module is imported multiple times in a program, Python by default _______ the module.
- ignores
- reimports
- reloads
- renames
If a module is imported multiple times in a program, Python by default ignores the module. Once a module is imported, Python keeps track of it and doesn't reload or re-import it again, to avoid duplicate code and potential issues.
The method _______ returns the index of the specified value in the tuple.
- find()
- index()
- locate()
- search()
The index() method is used to find the index of a specified value within a tuple. It returns the first occurrence of the value's index. If the value is not found, it raises a ValueError.