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.
Which version of Python introduced the print() function as needing parentheses?
- Python 2.5
- Python 2.6
- Python 2.7
- Python 3.0
In Python 3.0, the print statement was replaced with the print() function, requiring the use of parentheses.
The time complexity of checking the existence of an element in a list is _______.
- O(1)
- O(log n)
- O(n)
- O(n^2)
The time complexity of checking if an element exists in an unsorted list is O(n), where 'n' is the number of elements in the list. It requires searching through the list linearly to find the element.
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.