0 votes
258 views

What does the following program output?

program module (input, output);
var
a:array [1...5] of integer;
i, j: integer;
procedure unknown (var b: integer, var c: integer);
var
i:integer;
begin
for i := 1 to 5 do a[i] := i;
b:= 0; c := 0
for i := 1 to 5 do write (a[i]);
writeln();
a[3]:=11; a[1]:=11;
for i:=1 to 5 do a [i] := sqr(i);
writeln(c,b); b := 5; c := 6;
end;
begin
i:=1; j:=3; unknown (a[i], a[j]);
for i:=1 to 5 do write (a[i]);
end;

| 258 views

## 2 Answers

0 votes
Best answer

We can assume that the code is using pass-by-reference or else there is no use in the parameters $b,c.$

program module (input, output);
var
a:array [1...5] of integer;
i, j: integer;
procedure unknown (var b: integer, var c: integer);
//pass by reference; b and a[1] have same address
//and c and a[3] have same address
var
i:integer;
begin
for i := 1 to 5 do a[i] := i;
b:= 0; c := 0
for i := 1 to 5 do write (a[i]); // prints 1, 2, 3, 4, 5
writeln();
a[3]:=11; a[1]:=11;
for i:=1 to 5 do a [i] := sqr(i);
writeln(c,b); // prints 121, 121
b := 5; c := 6;
end;
begin
i:=1; j:=3; unknown (a[i], a[j]); //pass by reference
for i:=1 to 5 do write (a[i]); //prints 5, 4, 6, 16, 25
end;

by Veteran (418k points)
+1 vote

Module is a predefined array and globally decleared

Say values of a[0]=7

a[1]=8

a[2]=14

a[3]=16

a[4]=9

Now,

At begin i=1,j=3

Now, calling procedure unknown where b=8,c=16

a[1]=1,a[2]=2,a[3]=3,a[4]=4,a[5]=5 // As here no array declared, this array value will change global array value

b,c is assigned as 0.

a[3]=11,a[1]=11

a[1]=11,a[2]=2,a[3]=11,a[4]=4,a[5]=5

Next line all are squaring

a[1]=121,a[2]=4,a[3]=121,a[4]=16,a[5]=25

b is overwrite with value 5, c with value 6

Now, it is returning to main b=5,c=6

a[1]=5,a[3]=6 //main() have no array defined, so changing global array value

Global array now containing 121,5,121,6,25

In main it is printing 121,5,121,6,25

by Veteran (115k points)
edited by
0
See the question now..

+7 votes
2 answers
1
+12 votes
4 answers
2
+2 votes
1 answer
3
+12 votes
1 answer
4
+2 votes
3 answers
5
+13 votes
1 answer
6
+5 votes
1 answer
7