In a program processing a list of numbers, the objective is to print all numbers until a negative number is encountered. Which control statement would be apt for this task?

  • break
  • for
  • if
  • while
A while loop is apt for this task because it allows you to repeatedly check if the current number is negative and, if not, print it. When a negative number is encountered, the loop terminates, ensuring only non-negative numbers are printed.

In Python, the _______ method is used to overload the + operator.

  • __add__
  • __init__
  • __main__
  • __str__
In Python, the __add__ method is used to overload the + operator for user-defined classes. This method is called when the "+" operator is applied to objects of the class.

Which of the following loop types executes at least once, irrespective of the test condition?

  • do-while loop
  • for loop
  • until loop
  • while loop
A do-while loop in Python executes at least once, irrespective of the test condition, as the condition is checked after the loop body.

What does the finally block represent in Python's exception handling?

  • A block for catching exceptions
  • A block for cleaning up resources
  • A block for defining custom exceptions
  • A block for handling exceptions
The 'finally' block is used for cleaning up resources, such as closing files or network connections, regardless of whether an exception was raised or not.

If you wish to make a shallow copy of a list, you can use the _______ method.

  • clone() Method
  • copy() Method
  • deepcopy() Method
  • slice() Method
To make a shallow copy of a list, you can use the 'copy()' method. It creates a new list containing references to the same objects as the original list.

If you want to make a variable defined in an outer function accessible to an inner function, you use the _______ keyword.

  • global
  • inner
  • outer
  • public
To make a variable defined in an outer function accessible to an inner function, you use the 'global' keyword. It indicates that the variable should be treated as a global variable.

Which special variable in Python represents the name of the current module?

  • __current__
  • __main__
  • __module__
  • __name__
The special variable __name__ represents the name of the current module in Python. When a Python script is run, the value of __name__ is set to '__main__', indicating that it's the main script being executed. In imported modules, __name__ is set to the module's name. It's often used to check if a script is run as the main program or imported as a module.

The _______ file can be used to execute initialization code for a package.

  • init.init
  • init.py
  • init.txt
  • package.py
The __init__.py file is used to execute initialization code for a package. It runs when the package is imported or used, allowing you to set up package-level configurations or variables.

You are building a recommendation engine and have a set of products bought by User A and another set for User B. To recommend new products to User A based on User B's purchases, which set operation would you use considering you don't want to recommend products User A has already bought?

  • Difference
  • Intersection
  • Symmetric Difference
  • Union
To recommend new products to User A based on User B's purchases while avoiding duplicates, you would use the "Difference" set operation. This will provide products in User B's set that are not in User A's set.

To create a tuple with a single item, you need to add a _______ after the item.

  • Colon
  • Comma
  • Period
  • Semicolon
To create a tuple with a single item, you need to add a comma after the item. For example, my_tuple = (42,). A trailing comma distinguishes a single-item tuple from an expression in parentheses.