724 views

What does the following code do?

var a, b: integer;
begin
a:=a+b;
b:=a-b;
a:a-b;
end;
1. exchanges $a$ and $b$
2. doubles $a$ and stores in $b$
3. doubles $b$ and stores in $a$
4. leaves $a$ and $b$ unchanged
5. none of the above

edited | 724 views
0
typo mistake in a:a-b; it should be a:=a-b;

Answer is simply $A$ i.e. it swaps the values of the two.. Take any two values for $A$ and $B$. and perform the given operations over them.
by Boss (19.9k points)
edited
(a)
initially a = 10 b = 5
a = 15
b =  10
a = 5
by Boss (44.1k points)
+1 vote

1. a= a+b

2. b= a-b= (a+b)-b= a (from 1)

3. a= a-b= (a+b) - (a) = b (from 1 and 2)

Hence the values are getting exchanged

by Active (2.3k points)