Python was named after the British comedy show "Monty Python's _______ Circus".

  • Comedy
  • Flying
  • Hilarious
  • Silly
Python was named after the British comedy show "Monty Python's Silly Circus." The creators found inspiration in the show's humor and absurdity.

When a package is imported, Python searches for _______ to determine the package's initialization.

  • init.py
  • main.py
  • package.py
  • startup.py
When a package is imported, Python searches for the __init__.py file to determine the package's initialization. This file is executed when the package is imported.

Which of the following set methods does not modify the set but returns a new set?

  • difference()
  • intersection()
  • issubset()
  • union()
The union() method returns a new set that contains all the elements from both sets without modifying the original sets.

When using the setdefault method on a dictionary, what happens if the provided key already exists in the dictionary?

  • A new key-value pair is added with the default value
  • An error is raised
  • The existing key-value pair is overwritten with the default value
  • The existing value is returned
When setdefault is called with an existing key, it doesn't modify the existing value but returns it. If the key doesn't exist, it adds the key with the provided default value.

What keyword is used to define a function in Python?

  • def
  • define
  • func
  • function
In Python, the keyword used to define a function is 'def.' You start a function definition with 'def,' followed by the function name and its parameters.

The counterpart to the @property decorator for setting the value of an attribute is _______.

  • @attribute.setter
  • @property.setter
  • @value.setter
  • @set.setter
The counterpart to the @property decorator for setting the value of an attribute is @attribute.setter. It allows you to define a method that sets the value of the corresponding attribute.

You have a dictionary of student names and their scores. How would you get a list of student names sorted by their scores in descending order?

  • sorted_names = sorted(students.items(), key=lambda x: x[1])
  • sorted_names = sorted(students.items(), key=lambda x: x[1], reverse=True)
  • sorted_names = students.items().sort(key=lambda x: x[1])
  • sorted_names = students.items().sort(key=lambda x: x[1], reverse=True)
The correct way to sort the dictionary by scores in descending order is to use the sorted() function with a lambda function as the key argument. The lambda function extracts the score (value) from each (key, value) pair and sorts the items accordingly.

How can you access the last element of a list named my_list?

  • my_list.last()
  • my_list[-1]
  • my_list[0]
  • my_list[len(my_list) - 1]
You can access the last element of a list in Python by using negative indexing, as in "my_list[-1]". Negative indices count from the end, with -1 being the last element.

How can you emulate a do-while loop, given that Python does not have a built-in do-while construct?

  • Use a for loop with a conditional statement that controls the loop's execution.
  • Use a repeat loop construct.
  • Use a while loop with a conditional statement that checks the loop's termination condition at the beginning of the loop body.
  • Use a while loop with a conditional statement that checks the loop's termination condition at the end of the loop body.
To emulate a do-while loop in Python, you can use a while loop with a conditional statement that checks the loop's termination condition at the end of the loop body. This ensures that the loop body is executed at least once before checking the condition, similar to the behavior of a do-while loop in other languages.

While iterating through data entries, you want to avoid processing entries labeled as 'SKIP.' Instead of terminating the loop upon encountering such entries, what should you use to continue to the next entry?

  • break
  • continue
  • pass
  • return
To continue to the next entry without terminating the loop, you should use the continue statement. It skips the current iteration and moves on to the next one in the loop, allowing you to bypass processing entries labeled as 'SKIP' and continue with the rest of the loop.

When you want a variable to be available both inside and outside a function, you declare it as a _______ variable.

  • Global
  • Instance
  • Local
  • Non-local
To make a variable accessible both inside and outside a function, you declare it as a 'global' variable. It has a global scope.

Which of the following is not a valid list comprehension?

  • [x for x in range(10)]
  • [(x, x**2) for x in y]
  • [x if x % 2 == 0 else 'odd' for x in range(10)]
  • [x if x > 5 else for x in range(10)]
The fourth option is not a valid list comprehension because it's missing the expression that should follow the else keyword. In list comprehensions, the else part should have an expression to be evaluated when the condition is False. The correct form should be like: [x if x > 5 else for x in range(10)].