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.
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.
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.
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.
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.
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.
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.
In a banking application, you have an Account class. If you want to set a minimum balance for all accounts regardless of their type, where would you define this value?
- As a class attribute
- In a separate module
- In a subclass
- In the constructor (__init__)
To set a minimum balance for all accounts regardless of their type, you should define this value as a class attribute. Class attributes are shared among all instances of the class, ensuring that the minimum balance is consistent for all accounts.
While organizing your project, you decide to structure related modules under one directory. You find that importing them isn't as straightforward. What must be ensured for smoother imports?
- Each module should have a unique name.
- The directory containing the modules should include an __init__.py file to make it a package.
- The modules should be placed in different directories to avoid naming conflicts.
- Use absolute imports instead of relative imports.
To structure related modules under one directory, you should ensure that the directory is treated as a package by including an __init__.py file. This enables smoother imports using relative import paths, maintaining organization.
If you see the statement if name == "_______":, it's checking if the module is being run as the main program.
- execute
- init
- main
- module
If you see the statement "if name == 'main':", it's checking if the module is being run as the main program. This is a common way to include code that runs only when the module is run directly.