How does Python store tuples internally that allows for their hashable property?

  • Tuples are stored as a linked list.
  • Tuples are stored as a sequence of bytes.
  • Tuples use a hash function.
  • Tuples use a unique identifier.
Python stores tuples internally as a sequence of bytes, and this immutability allows them to be hashable. The hash value is computed from the content of the tuple, making them suitable for use as keys in dictionaries.

The process of assigning a value to a variable for the first time is called variable _______.

  • Assignment
  • Declaration
  • Definition
  • Initialization
The process of assigning a value to a variable for the first time is called variable initialization.

You have a list data = [1, 3, 5, 7, 9]. You need to add the numbers 2, 4, 6, 8 to the list such that the list remains sorted. Which approach will be most efficient?

  • data += [2, 4, 6, 8]
  • data.extend([2, 4, 6, 8])
  • data.insert(2, [2, 4, 6, 8])
  • data.sort()
The most efficient approach is to use 'data += [2, 4, 6, 8]' as it directly appends the sorted numbers to the list, maintaining the sorted order.

You are writing a Python script on a new system and realize it doesn't have the required module. Which command would you use to install this module?

  • install-module module-name
  • pip install module-name
  • py-install module-name
  • python install module-name
You would use pip install module-name to install the required Python module. pip is the standard package installer for Python, and it allows you to easily download and install Python packages from the Python Package Index (PyPI).

You want to create a logging utility that can be used across various modules in your project without repeatedly initializing the logger. Which Python feature can help in achieving this?

  • Python Decorators
  • Python Logging Module
  • Python Modules and Imports
  • Python's Global Variables
In Python, you can create a logging utility by using decorators. You can wrap your functions with a custom decorator that initializes the logger and allows you to use it across modules without repetitive initialization.

What is the difference between *args and **kwargs in Python function definitions?

  • *args and **kwargs are identical and can be used interchangeably.
  • *args is used for keyword arguments, and **kwargs is used for positional arguments.
  • *args is used for positional arguments, and **kwargs is used for keyword arguments.
  • *args is used for unpacking an iterable, and **kwargs is used for multiple return values.
In Python function definitions, *args is used to pass a variable-length list of positional arguments, while **kwargs is used to pass a variable-length list of keyword arguments. They allow for flexibility in function parameters.

What is the primary purpose of the init.py file in a Python package?

  • To define package-level variables
  • To indicate that the package is executable
  • To make the directory a Python package
  • To serve as the package's documentation
The init.py file is used to make a directory a Python package. It allows you to organize related modules into a single package, making it easier to import and manage them. Python versions 3.3 and later do not require this file, but it's still good practice for compatibility.

If you need to manage multiple resources simultaneously, the contextlib module provides a utility named _______ to nest context managers.

  • ExitStack
  • context_manager
  • context_stack
  • nested_context
The contextlib module provides a utility named ExitStack for nesting context managers. It allows you to manage multiple resources simultaneously by entering and exiting them in a stacked manner, ensuring proper cleanup.

How can you specify a default value for a parameter in a function definition?

  • By wrapping the parameter in square brackets []
  • Using an asterisk (*)
  • Using the '=' sign
  • Using the 'default' keyword
To specify a default value for a parameter, you can use the '=' sign followed by the default value. For example, def func(param=10):.

You need to filter out all even numbers from a list numbers and square them. Which Python feature would be the most concise to achieve this?

  • Filter and lambda functions
  • For loop
  • List comprehensions
  • Map and lambda functions
List comprehensions are the most concise way to filter and manipulate elements in a list. You can achieve this with a one-liner using list comprehensions.