A colleague is seeing unexpected behavior in a function. The function uses a list as a default argument and modifies it. Every time the function is called without this argument, the list seems to retain its changes. What's the likely cause?
- Caching of previous function calls
- Incorrect usage of global variables
- Python's behavior with mutable default arguments
- The list is defined as a class attribute
The likely cause is Python's behavior with mutable default arguments. The list retains its changes because it's a mutable object shared among calls.
Which dictionary method provides a view on dictionary's items in the form of tuples?
- dict.items()
- dict.tuples()
- dict.values()
- dict.viewitems()
The dict.items() method returns a view object that displays a list of a dictionary's (key, value) tuple pairs, making it useful for iterating through a dictionary's items.
What is the primary use of the pass statement in Python?
- To act as a placeholder
- To indicate a successful run
- To skip a loop iteration
- To terminate the program
The primary use of the 'pass' statement in Python is to act as a placeholder. It does nothing when executed and is often used as a temporary placeholder for code that will be implemented later.
Which of the following is not a Python standard library?
- math
- numpy
- os
- stdlib
numpy is not a Python standard library; it's a third-party library commonly used for numerical and scientific computing. Python's standard libraries include os and math.
Which of the following is the correct way to create a list in Python?
- list = "1, 2, 3, 4"
- list = (1, 2, 3, 4)
- list = [1, 2, 3, 4]
- list = {1, 2, 3, 4}
The correct way to create a list in Python is by using square brackets, as shown in "list = [1, 2, 3, 4]". Square brackets denote a list in Python.
Which file mode in Python allows appending to a file in binary format?
- 'a'
- 'a+'
- 'ab'
- 'rb+'
The 'ab' file mode allows appending to a file in binary format. When you open a file in 'ab' mode, data is written in binary, making it suitable for binary file types like images or non-text data.
When you import a module using an alias, which keyword do you use?
- as
- importas
- rename
- using
When you import a module using an alias, you use the 'as' keyword. This allows you to create a shorter or more descriptive name for the module, making your code more readable and avoiding naming conflicts.
The pass statement in Python is essentially a _______ operation, meaning it doesn't perform any action.
- Break
- Continue
- Halt
- No-op
The 'pass' statement is a no-op or a no-operation statement. It is used as a placeholder when syntactically required but no action is desired. It doesn't perform any operation.
If my_list = [10, 20, 30, 40], what does the expression my_list[-2::-2] evaluate to?
- [20, 10]
- [30, 10]
- [40, 20]
- [40, 30]
The expression my_list[-2::-2] starts at the second-to-last element of the list (which is 30), and it then steps backward by 2 positions, so it selects every second element in reverse order. Therefore, the result is [20, 10].
When you want to give a different name to a module upon importing, you use an _______.
- Import As
- Import Renaming
- Module Alias
- Module Alias Renaming
When you want to give a different name to a module upon importing, you use an "Import As" statement. It allows you to use a shorter or more convenient name when working with the module.
A software system reads data from various sources. Whenever there's corrupted data, you want to log it and continue processing other data. How would you design the exception handling mechanism?
- Ignore corrupted data completely and proceed with the remaining data without logging.
- Implement a separate error-handling thread to handle data corruption without stopping processing.
- Stop processing data as soon as corruption is detected to prevent further issues.
- Use try-except blocks to catch and log exceptions, then continue processing unaffected data.
The correct approach is to use try-except blocks to catch and log exceptions while continuing to process unaffected data. This ensures data integrity and allows for identifying and handling corrupted data while not halting the entire process.
If you're working with both CSV and JSON data formats in a project, which Python standard library module would be redundant?
- codecs
- csv
- json
- pickle
The 'codecs' module is not necessary when working with CSV and JSON data formats. It is used for character encoding and decoding, which is not directly related to handling CSV and JSON files. You would typically use 'csv' and 'json' modules for those tasks.