Question 1Easy6 marks
Look at this program.
01 a = 8
02 b = 3
03 temp = a
04 a = b
05 b = temp
06 print(a, b)(a) Complete the trace table in {table} to show the values of the variables after each line has run.[4]
[object Object]
(b) State the output of the program.[1]
(c) State the purpose of the variable
temp.[1]Show the answer and mark scheme
(a) Answer: 02: a = 8, b = 3; 03: temp = 8; 04: a = 3; 05: b = 8 (so a = 3, b = 8, temp = 8 at the end)
- line 02: b = 3 (a is still 8)
- line 03: temp = 8
- line 04: a = 3
- line 05: b = 8
(b) Answer:
3 83 8
(c) Answer: It keeps a copy of the original value of a. Without it, this value would be lost when line 04 runs, so the values could not be swapped.
- to keep a copy of the original value of
a(which is overwritten in line 04) so the values can be swapped