edited by
1,725 views
4 votes
4 votes

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(a[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;
edited by

2 Answers

Best answer
6 votes
6 votes

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 0, 2, 0, 4, 5
    writeln();
    a[3]:=11; a[1]:=11;
    for i:=1 to 5 do a [i] := sqr(a[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;

 

edited by
1 votes
1 votes

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

edited by

Related questions

17 votes
17 votes
2 answers
1
makhdoom ghaya asked Nov 23, 2016
4,291 views
State whether the following statements are TRUE or FALSE with reason:The Link-load-and-go loading scheme required less storage space than the link-and-go loading scheme.
17 votes
17 votes
3 answers
2
makhdoom ghaya asked Nov 19, 2016
8,353 views
Match the pairs in the following questions:$$\begin{array}{ll|ll}\hline (a) & \text{Pointer data type} & (p) & \text{Type conversion} \\\hline (b) & \text{Activation rec...