search
Log In
12 votes
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
     
in Algorithms
edited by
1k views
0
typo mistake in a:a-b; it should be a:=a-b;

4 Answers

22 votes
 
Best answer
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 by
6 votes
(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

Answer(A).

0 votes
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
Answer:

Related questions

26 votes
6 answers
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)$
asked Sep 30, 2014 in Algorithms Kathleen 2.7k views
10 votes
3 answers
2
2.6k views
A part of the system software which under all circumstances must reside in the main memory is: text editor assembler linker loader none of the above
asked Sep 30, 2014 in Compiler Design Kathleen 2.6k views
19 votes
3 answers
3
6.1k views
A simple two-pass assembler does the following in the first pass: It allocates space for the literals. It computes the total length of the program. It builds the symbol table for the symbols and their values. It generates code for all the load and store register instructions. None of the above.
asked Sep 30, 2014 in Compiler Design Kathleen 6.1k views
16 votes
2 answers
4
2k views
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
asked Sep 30, 2014 in Set Theory & Algebra Kathleen 2k views
...