What is the value of 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.
View worked solution
Answer: A
In Pascal, parameters are passed by value by default, unless they are explicitly declared with the var keyword (which denotes call-by-reference).
Let's trace the execution of the program:
- The global variable
Xof typeintegeris initialized to2in the main program block:X := 2; - The procedure
FIND(X)is called. Since the parameter inprocedure FIND (X:real)is passed by value, a local copy ofXis created for the procedure. - Inside
FIND, the local variableXis updated to :
This modification is local to the procedure and does not affect the global variableX := sqrt(X);X. - After the procedure call terminates, control returns to the main program, where
writeln(X)is executed. - Since the global variable
Xremains unchanged, the value printed is 2.
Therefore, the correct option is A.
Free practice · Your answers on this page are not saved.
