The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
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;
in Compiler Design by Boss (29.7k points) | 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..

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,092 questions
55,276 answers
190,803 comments
86,086 users