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.

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

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.

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.

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.

Which memory management technique does Python primarily utilize?

  • Garbage Collection (GC)
  • Manual Memory Management (malloc/free)
  • Reference Counting
  • Stack Allocation
Python primarily utilizes Garbage Collection (GC) for memory management. It automatically deallocates memory occupied by objects that are no longer in use, making memory management more convenient and reducing the risk of memory leaks.

Which Python module provides functionality to read and write data in CSV format?

  • csv
  • dataio
  • table
  • textfile
The csv module in Python provides functionality for reading and writing data in CSV (Comma-Separated Values) format, making it a popular choice for working with spreadsheet-like data.

The file method _______ is used to obtain the current position of the file read/write pointer.

  • current_position()
  • file_position()
  • get_position()
  • tell()
The tell() method in Python's file handling is used to obtain the current position of the file read/write pointer. It returns an integer representing the current position in bytes. This is crucial when you want to navigate through a file or keep track of where you are when reading or writing data.

When iterating over a dictionary's items, each item is presented as a _______.

  • Key
  • Key-Value Pair
  • Tuple
  • Value
When iterating over a dictionary, each item is presented as a key-value pair (a tuple), where the key represents the dictionary's key, and the value represents the corresponding value associated with that key.

Which decorator in Python is commonly used to define a method as a class property getter?

  • @get
  • @getter
  • @method
  • @property
To define a method as a class property getter in Python, the @property decorator is commonly used. This decorator allows you to access a method like an attribute, making it a clean way to implement read-only properties in classes.