In Python, a multi-line string can be defined using _______ characters.

  • Double quotes
  • Single quotes
  • Triple quotes
  • Triple single-quotes
In Python, a multi-line string can be defined using triple single-quotes ('''...''') or triple double-quotes ("""...""").

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.

What's the primary difference between from module import * and import module as alias?

  • Both statements are identical and can be used interchangeably.
  • The former imports all names from the module, polluting the namespace.
  • The former is faster in terms of execution.
  • The latter allows selective access to module members using the alias.
'from module import *' imports all names from the module into the current namespace, potentially causing naming conflicts. 'import module as alias' imports the module with a specified alias, offering more controlled access to its members.

What does the *args notation in function parameters allow for?

  • Defining keyword arguments
  • Passing a variable number of arguments
  • Restricting the number of function calls
  • Specifying the return type of the function
The *args notation allows a function to accept a variable number of non-keyword arguments. These arguments are packed into a tuple, allowing flexibility in function calls. It's often used when the exact number of arguments is unknown in advance.

What could be a practical scenario where the pass statement becomes essential, especially in function definitions?

  • Acts as a comment
  • Placeholder for future code
  • Signals a syntax error
  • Terminates the program execution
The 'pass' statement is used as a placeholder for future code in situations where a function or block of code must exist but has no implementation yet.

Use the 'with' statement to open files, which will automatically close the file when it goes out of scope.

  • Avoid using file handlers altogether
  • Depend on the operating system to close it
  • Manually close the file at the end of script
  • Use 'finally' block to close the file
Using the 'with' statement ensures that the file is automatically closed when it's no longer needed, preventing resource leakage.