Question 17
What is the output of the following code? [3 marks]
Solution
Initialize ali, abu, achan with values 3, 7 and 4 respectively.
From here, variable tom is initialized with result of 10 * 2 / 2 // 1.
10 * 2 / 2 // 1
= 20 / 2 // 1
= 10.0 // 1
= 10.0 # if either side of the integer division operator is a floating point number, the result becomes a floating point number
Hence, tom = 10.0.
We now initialize l_ori = [ali, abu, achan], which then equates [3, 7, 4].
l_copy is then initialized with l_ori passed by reference.
Because the contents of l_ori are passed by value from their original sources, this means that any further changes to ali, abu and achan do not affect their copies in l_ori.
Hence, ali = 1 does nothing to l_ori.
However, because the contents in l_copy are passed by reference from l_ori, if at all the contents of either one of these two lists are modified, the modifications are reflected in the other.
Here, since we modified l_ori to append the value of tom at the end, l_ori and l_copy reflect this modification.
Both of these lists' values equal [3, 7, 4, 10.0].