Skip to content
ExamSpine

Lesson 8.1 · Trace it

Trace: two objects

Python code3 rows to fill3 marks

Complete the trace table for this Python code. Jar is a class. a and b are objects made from it, and sweets is an attribute that each of them keeps. Then answer the question below the table. [3]

Line 1: class Jar:
Line 2, indented 1 level: pass
Line 3:
Line 4: a = Jar()
Line 5: a.sweets = 4
Line 6: b = Jar()
Line 7: b.sweets = 9
Line 8: a.sweets = a.sweets + 3
Line 9: b.sweets = b.sweets - a.sweets
Line 10: x = a.sweets
Line 11: a.sweets = 0
Line 12: y = b.sweets + a.sweets
Line 13: print(x + y)

Fill in one row each time a line changes x or y, or prints something. The table already has the right number of rows. In each row, write only what that line changes; you can leave the other cells empty or copy their values down. Write values the way Python shows them: 3 and 3.0 are different, and text goes in quotes. In the output column, write exactly what appears on screen, with no quotes around it.

Rowxyoutput
1
2
3