The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+7 votes

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

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

4 Answers

+12 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
but no where mentioned that call by value is in use here.
yaa, they have not mentioned anything about any parameter passing technique.

then which technique should we take default, if they have not mentioned anything?
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.
ok, thank you.
Can we pass an integer value to a function having real parameter? Wouldn't it give an error?

@Arjun sir . @Shaik static scoping..which is the default case...why not the answer is square root 2... since variable definition is not found in local scope so global variable is considered...

 In similar ques ..we have used this feature  static scoping

+2 votes
2..because it is call by value doesn't gets changed.
answered by Active (2.8k points)
Can you know explain me?
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.
+1 vote
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 (435 points)
0 votes
It goes into an infinite loop as the 2nd prog. goes on calling the 1st prog. And it never reaches the writeline itdoesnt print anything
answered by Boss (14.4k points)
It is calling only once rt?
sir, isn't the correct option should be b).only..because the function is being called only once...
Yes. But there can also be a compile time error (in strongly typed languages) since an integer is passed to a function expecting real.
how infinite loop?
In strongly typed languages, during run-time the type will be verified right?

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
49,540 questions
54,099 answers
71,007 users