Skip to content
Official PYQ · 2001

GATE CS 2001 Q2.17 — Programming in C

Explain with AI

What is printed by the print statements in the program P1P1 assuming call by reference parameter passing?

Program P1()
{
    x = 10;
    y = 3;
    func1(y,x,x);
    print x;
    print y;
}

func1(x,y,z)
{
    y = y + 4;
    z = x + y + z
}
Choose one option
View worked solution

Answer: B

In call-by-reference parameter passing, the formal parameters of the called function become aliases (references) to the actual arguments passed from the calling function. Any modification to the formal parameters directly updates the actual arguments.

1. Mapping Parameters

In P1(), we have the variables:

  • x = 10
  • y = 3

The function call is func1(y, x, x). In func1(x, y, z), the formal parameters map to the actual variables as follows:

  • Formal parameter x is a reference to actual variable y of P1.
  • Formal parameter y is a reference to actual variable x of P1.
  • Formal parameter z is also a reference to actual variable x of P1.

Thus, both y and z in func1 are aliases for the same variable x in P1.

2. Step-by-Step Execution of func1

  • Statement 1: y = y + 4;

    Since formal y refers to P1.x:

    P1.x=P1.x+4=10+4=14\text{P1.x} = \text{P1.x} + 4 = 10 + 4 = 14\n

    Now, P1.x becomes 1414. Since formal z also refers to P1.x, the value accessed via z is now also 1414.

  • Statement 2: z = x + y + z;

    Substituting the references (formal x refers to P1.y, and formals y and z refer to P1.x):

    P1.x=P1.y+P1.x+P1.x\text{P1.x} = \text{P1.y} + \text{P1.x} + \text{P1.x} P1.x=3+14+14=31\text{P1.x} = 3 + 14 + 14 = 31

3. Final Output

After returning to P1():

  • print x; outputs 31
  • print y; outputs 3

Therefore, the printed values are 31, 3.

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

Published
Report a correction
Keep exploringMore Programming in C questions