The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+4 votes
1.2k views

What is the value of $X$ printed by the following program?

program COMPUTE (input, output);
var X:integer;
procedure FIND (X:real);
    begin
        X:=sqrt(X);
    end;
begin
    X:=2
    FIND(X);
    writeln(X);
end.
  1. $2$
  2. $\sqrt{2}$
  3. Run time error
  4. None of the above
asked in Compiler Design by Veteran (59.7k points)
edited by | 1.2k views

4 Answers

+10 votes

i think ans should be A.

as per call by value concept. $X$ in the procedure FIND is a local variable. no change will be refleced in global var $X$.

answered by Loyal (8.1k points)
edited by
0
but no where mentioned that call by value is in use here.
0
yaa, they have not mentioned anything about any parameter passing technique.

then which technique should we take default, if they have not mentioned anything?
+5
If that happens mark will be given for all. For this question, it might have been mentioned but lost while the question was copied. We don't have any original GATE question paper before 2007. So, those questions available online might not be exactly the same as were asked in GATE.

If the code is C language, then you must assume call-by-value.
0
ok, thank you.
0
Can we pass an integer value to a function having real parameter? Wouldn't it give an error?
+2 votes
2..because it is call by value..so value doesn't gets changed.
answered by Active (2.9k points)
0
Can you know explain me?
+1
Here we are not passing the address of x.So it does not get changed.Only a copy of it is made thats why it is 2.
0 votes
It goes into an infinite loop as the 2nd prog. goes on calling the 1st prog. And it never reaches the writeline ..so itdoesnt print anything
answered by Boss (14.4k points)
0
It is calling only once rt?
0
sir, isn't the correct option should be b).only..because the function is being called only once...
+1
Yes. But there can also be a compile time error (in strongly typed languages) since an integer is passed to a function expecting real.
0
how infinite loop?
0
In strongly typed languages, during run-time the type will be verified right?
0 votes
The program is in Pascal, for which default function call is call-by-value, so value 2 is passed to the procedure FIND which performs operation on it's local variable X (which is defined as real) therefore changes would not be reflected on the global variable, the value of X=2 will thus be written/printed.
answered by (303 points)

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

44,456 questions
49,912 answers
165,384 comments
65,897 users