Multiple choice questions with diagnostic answers
Both of my multiple choise questions are building on the lessons about Making Choices (Python).
Suppose we define the following tuple
a and list
b as follows:
a=(1,2,3,4) id_number_a = id(a) a+=(5,6,7) id_number_a_extended = id(a) b=[1,2,3,4] id_number_b = id(b) b+=[5,6,7] id_number_b_extended = id(b)
Which one of the following statements will return True?
a == b
id_number_a == id_number_a_extended
id_number_b == id_number_b_extended
id_number_a == id_number_b
id() returns the “identity” of an object which might change during the lifetime of the variable (see documentation).
The following code should sort and compute the power of each of the numbers within a list.
list = [3,5,1,2,4] list_2 = [???]
Think about it and complete the
??? by choosing one of the following options:
[x*x for x in list.sort()]
[x*x for x in sorted(list)]
[x^2 for x in sorted(list)]
[x**2 for x in list[:].sort()]
list.sort() is a method that mutes the object whereas
sorted(list) is a
function that doesn’t.
What can the different answer tell us about misconception of the student?
- The student does not understand the concept of immutability and chooses this method which modifies the list on the fly (and in fact returns an error).
- The correct answer
- The student does not remember the exponentiation operator in Python.
- The student fails with the same concept of the point a) and confuses the manipulations of lists being objects.