retagged by
1,638 views
8 votes
8 votes

Consider the following program fragment :

int foo(int ,int );
int a;
int ar[4] = { 1,0, 2,3};
main()
{
a = 0;
foo(ar[a], ar[ar[a]]);
printf("%d,%d ,%d,%d ", ar[0] , ar[1] ,ar[2] , ar[3]);
}


foo(int x, int y)
{
x= x+1;
y=y+1;
x=x+1;
y=y+1;
ar[1] =50;
}

What value will be printed by the program if parameter passed by call by name?

  1. $1, 50, 2, 3$
  2. $3, 50, 2, 3$
  3. $3, 50, 3, 4$
  4. $3, 2, 2, 3$
retagged by

2 Answers

Best answer
19 votes
19 votes

Call by name is same as macro

In this question, ar[a] corresponds to x & ar[ar[a]] corresponds to y.

Now, x=x+1;

That means ar[a]=ar[a]+1;

                    ar[0]=ar[0]+1;

                    ar[0]=1+1;

                    ar[0]=2;

            Now y= y+1;

                   ar[ar[a]]=ar[ar[a]]+1;

                   ar[ar[0]]=ar[ar[0]]+1;

                   ar[2]=2+1;

                    ar[2]=3;

              Now, again x=x+1;

                        ar[0]=ar[0]+1;

                    ar[0]=2+1;

                    ar[0]=3;

Now y= y+1;

                   ar[ar[a]]=ar[ar[a]]+1;

                   ar[ar[0]]=ar[ar[0]]+1;

                   ar[3]=ar[3]+1;

                    ar[3]=3+1;

                     ar[3]=4;

               ar[1]=50; (given in the query)

Therefore ,  option c matches.

selected by
0 votes
0 votes

Ans should be option A . 
under the function call nothing will get affected other that value of ar[1] , which is made to 50 . 

so final ans should be 1, 50 , 2, 3
check the output here : http://ideone.com/P3LKiW

Answer:

Related questions

5 votes
5 votes
1 answer
2