In which block do you write the code that might raise an exception?

  • except
  • finally
  • raise
  • try
You write the code that might raise an exception in the try block in Python. The try block contains the code that you want to monitor for exceptions. If an exception occurs, it is caught by the except block.

In Python, every function returns a value. If no return statement is present, it returns _______ by default.

  • 0
  • FALSE
  • None
  • TRUE
In Python, if no return statement is present in a function, it returns 'None' by default. 'None' is a special value representing the absence of a value.

The concept where the inner function captures and remembers the enclosing function's local state is called _______.

  • Abstraction
  • Closure
  • Encapsulation
  • Inheritance
The concept where the inner function captures and remembers the enclosing function's local state is called a Closure. Closures are used in Python to create functions with behavior that depends on the variables in the enclosing function.

To make the custom objects of a class iterable using a for loop, one should implement the _______ and _______ methods.

  • __for__ and __loop__
  • __iter__ and __next__
  • __iterator__ and __iter__
  • __next__ and __previous__
To enable custom objects of a class to be iterable, you need to implement the __iter__ and __next__ methods. The __iter__ method returns the iterator object itself, and the __next__ method defines how to iterate over the object.

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.

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.

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.

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.

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.

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.