678 views
3 votes
3 votes

a) 4 b)20 c)both d) either of them ... for me its 20 but given ans is either of them 

1 Answer

Best answer
2 votes
2 votes

Here few things to be kept in mind :

a) Copy and restore parameter passing technique is modification of call by reference technique.

b) The difference being : the passed variable in the function is not modified till the last statement of the function..After the entire function executes , then the final value of corresponding function variable(formal argument) is passed into the passed variable from the calling function(also known as actual argument)..So we have to wait till last statement in the function..

c) While in call by reference if the updation is done in a variable that is reflected immediately..

Let's see the given code..

a)  The variable 'a' is passed to the function having value = 5..

b)   This value is copied to both function variables 'x' and 'y' and hence each has value = 5..

c)   After the step :  x--  ,  x is decremented to 4 but it has no impact on value of 'a' as it is not final statement..The value of 'a' is still 5..

d)  Now y = y + 15 is done which leads y = 20..As this is the final statement and we copied the value of 'a' into 'y' initially , hence we restore this value of 'y' back into 'a'.

Thus final value of 'a' = 20..Hence B) should be correct option..

Suggested reading : https://stackoverflow.com/questions/8848402/whats-the-difference-between-call-by-reference-and-copy-restore 

selected by

No related questions found