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.
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 _______ 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.
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.