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.

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.

Which of the following symbols is used to define a tuple in Python?

  • ( )
  • < >
  • [ ]
  • { }
In Python, tuples are defined using parentheses ( ). For example, my_tuple = (1, 2, 3) creates a tuple named my_tuple.

Which Python Enhancement Proposal (PEP) introduces the concept of "The Zen of Python"?

  • PEP 20
  • PEP 42
  • PEP 64
  • PEP 8
"The Zen of Python" is introduced in PEP 20. It encapsulates the guiding principles and philosophy of Python's design.

In Python, to create a private attribute in a class, you prefix the attribute name with _______.

  • @private
  • __private
  • _private
  • private:
In Python, a private attribute is created by prefixing the attribute name with a double underscore, like "__private".

Which Python feature allows functions to accept any number of keyword arguments?

  • **args
  • **arguments
  • **kwargs
  • *arguments
The feature in Python that allows functions to accept any number of keyword arguments is **kwargs. This is often used when you want to pass a variable number of keyword arguments to a function. The double asterisk ** before kwargs allows you to collect keyword arguments into a dictionary within the function.

You've received a JSON file with non-ASCII characters, and while reading the file using the Python json module, you're facing an encoding issue. What can be done to correctly parse the JSON?

  • Convert the non-ASCII characters to their Unicode equivalents.
  • Use a different JSON parsing library that supports non-ASCII characters.
  • Use the 'json.load' function with the 'encoding' parameter.
  • Use the 'open' function with an explicit encoding argument.
To correctly parse a JSON file with non-ASCII characters, you can use the 'open' function with an explicit encoding argument to specify the correct character encoding used in the file.

When using a nested loop inside a list comprehension, the _______ loop should be specified first.

  • Inner
  • Outer
  • Primary
  • Secondary
When using nested loops in list comprehensions, the inner loop should be specified first. This is because the inner loop is executed more frequently and iterates over each element in the inner sequence, while the outer loop iterates over the outer sequence. The inner loop determines how the elements are constructed in the resulting list comprehension.