Which keyword is used in Python to check a condition?
- check
- if
- then
- verify
In Python, the 'if' keyword is used to check a condition. It allows you to execute a block of code if the condition is true.
When working with large files, why might the readlines() method not be the most memory-efficient?
- It doesn't work with large files
- It lacks support for binary file formats
- It reads the entire file into memory
- It's not available in Python
The 'readlines()' method reads the entire file into memory, which can be inefficient for large files, as it can lead to memory consumption issues. It's better to use methods like 'readline()' or iterate over the file to process it line by line.
In a for-loop iterating through a list, if a specific condition is met and you want to skip the remaining code in the current iteration but not exit the loop, you would use the _______ statement.
- Break
- Continue
- Pass
- Skip
The 'continue' statement is used to skip the remaining code in the current iteration of a loop and move to the next iteration without exiting the loop. So, you would use 'Continue.'
For which of the following OS is Python NOT available by default?
- Android
- Linux
- Windows
- macOS
Python is available by default on Windows, macOS, and Linux. However, it is not available by default on Android operating systems.
What keyword is used to stop the execution of the current iteration and proceed to the next in a loop?
- break
- continue
- skip
- stop
The 'continue' keyword is used to stop the execution of the current iteration in a loop and proceed to the next iteration. It allows you to skip specific iterations.
In a project where memory efficiency and speed are of utmost priority and the data doesn't need any modification once set, which data structure would be a better choice?
- Dictionary
- List
- Set
- Tuple
Tuples are an excellent choice when memory efficiency and speed are crucial because they are immutable and consume less memory compared to lists. Their immutability also ensures that data integrity is maintained, making them suitable for scenarios where data should not be modified after initialization.
When importing a module, what is the primary purpose of using the as keyword?
- To create an alias for the module
- To hide the module's functions
- To prevent the module from being imported again
- To specify the module's version
The 'as' keyword is used to create an alias for a module when importing it. This alias can make it easier to reference the module in your code, especially if the module name is long or conflicts with other names in your script.
You're reviewing a Python class and notice that many attributes are accessed and modified directly, without any checks. What could be introduced to provide controlled access and validation to these attributes?
- Creating a separate module to handle attribute access and modification.
- Defining the attributes within a function for validation.
- Encapsulating the attributes by making them private and providing getter and setter methods.
- Using global variables to track and validate attribute changes.
To provide controlled access and validation to attributes, you should encapsulate the attributes by making them private and providing getter and setter methods to enforce checks and validation.
You're working with a dictionary where keys are country names and values are their capitals. You want to invert this dictionary. What potential issue might you encounter?
- No potential issue, dictionaries are always invertible.
- Potential issue: Overwriting keys if country names are not unique.
- Potential issue: Overwriting values if capitals are not unique.
- Potential issue: Python dictionaries cannot be inverted.
When inverting a dictionary, if the values (capitals) are not unique, you may encounter an issue where some values get overwritten. This results in the loss of information, as multiple countries may have the same capital. Inverting dictionaries assumes unique values for keys in the original dictionary.
What is the primary reason to use method overriding in object-oriented programming?
- To create a new method
- To extend a class's functionality
- To hide a method's implementation
- To improve code readability
The primary reason to use method overriding is to hide a method's implementation in the base class and provide a specialized implementation in the derived class. This allows for polymorphism and abstraction.