Skip to content
Official PYQ · 1998

GATE CS 1998 Q2.13 — Programming in C

Explain with AI

What is the result of the following program?

    program side-effect (input, output);
    var x, result: integer;
    function f (var x:integer):integer;
    begin
        x:x+1;f:=x;
    end
begin
    x:=5;
    result:=f(x)*f(x);
    writeln(result);
    end
Choose one option
View worked solution

Answer: C

To understand the output of the program, let's analyze the behavior of the expression result := f(x) * f(x) where x is initially 5:

  • Standard Call-by-Reference: If the compiler evaluates the expression sequentially, the first call f(x) increments the global x to 66 and returns 66. The second call f(x) then increments the global x to 77 and returns 77, yielding 6×7=426 \times 7 = 42.
  • Call-by-Value-Result / Deferred Side-Effects: If the compiler uses call-by-value-result (copy-in, copy-out) or evaluates both operands before updating the global state, both calls to f(x) receive the initial value of x = 5. Each call increments its local copy to 66 and returns 66, yielding 6×6=366 \times 6 = 36.

Since C (3636) is the designated correct option, the evaluation model assumes call-by-value-result or deferred side-effect propagation where the global state is not updated between the operand evaluations.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Programming in C questions