What does the following code do?
var a, b: integer;
begin
a:=a+b;
b:=a-b;
a:=a-b;
end;View worked solution
Answer: A
Let the initial values of the variables and be and respectively.
We can trace the execution of the statements step-by-step:
a := a + b;
The new value of becomes:b := a - b;
Using the updated value of :a := a - b;
Using the updated values of and :
After the execution of these three statements, the final value of is and the final value of is . Thus, the values of and are exchanged (swapped) without using a temporary variable.
Therefore, the correct option is A.
Free practice · Your answers on this page are not saved.
