Variable Tracing Assignments
Share
Practice Question
a <- 5
b <- 10
c <- a + b
a <- c - a
b <- a + c
DISPLAY(a)
DISPLAY(b)Trace step-by-step: Initially a=5, b=10. Line 3: c <- 5+10, so c=15. Line 4: a <- 15-5, so a=10 (a is now 10, not 5). Line 5: b <- 10+15, so b=25 (using the NEW value of a, which is 10). First DISPLAY shows a=10. Second DISPLAY shows b=25. The key insight: once a changes in line 4, subsequent references to a use the NEW value (10), not the original (5).
A) This correctly identifies a=10 but miscalculates b. The error is computing b <- a + c as 5 + 15 = 20, using the OLD value of a (5) instead of the UPDATED value (10). After line 4 executes, a is 10, so b = 10 + 15 = 25.
C) This miscalculates both values. For a: the expression c - a uses c=15 and a=5, giving 10 (not 15). This answer suggests computing c - a as just c, forgetting to subtract. Check your arithmetic carefully.
D) This makes the same error as C for the first value (computing a as 15 instead of 10), then compounds it by using that incorrect value to calculate b. If a were 15, then b = 15 + 15 = 30, but a is actually 10.
The critical error is forgetting that variable assignments CHANGE the variable's value for all subsequent uses. When line 4 executes (a <- c - a), the variable a immediately becomes 10. Line 5 uses this NEW value, not the original 5. Always update your variable tracking table after EACH line!
For variable tracing: Create a table with columns for line number and each variable. Update the table AFTER each line executes. When a variable appears on the right side of <-, use its CURRENT value from your table. When it appears on the left, UPDATE its value in the table. This prevents using stale values.