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
Note that 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()]
Tip: 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?
Source: These exercises are based on Chema Cortés’ blog entries (1, 2).