# GATE1993-7.4

1k 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
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.

edited
(a)
initially a = 10 b = 5
a = 15
b =  10
a = 5
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

Exchanges a and b.
Let us consider a=5; b=2
a := a+b = 5+2 = 7
b := a-b = 7-2 = 5
a := a-b = 7-5 = 2
O/P: a=2; b=5

## Related questions

1
2.7k views
$\displaystyle \sum_{1\leq k\leq n} O(n)$, where $O(n)$ stands for order $n$ is: $O(n)$ $O(n^2)$ $O(n^3)$ $O(3n^2)$ $O(1.5n^2)$
Let A be a finite set of size n. The number of elements in the power set of $A\times A$ is: $2^{2^n}$ $2^{n^2}$ $(2^n)^2$ $(2^2)^n$ None of the above