GATE | GATE-CS-2003 | Question 74

The following program fragment is written in a programming language that allows variables and does not allow nested declarations of functions.

filter_none

edit
close

play_arrow

link
brightness_4
code

global int i = 100, j = 5;
void P(x)
{
    int i = 10;
    print(x + 10);
    i = 200;
    j = 20;
    print(x);
}
main()
{
    P(i + j);
}

chevron_right


If the programming language uses dynamic scoping and call by name parameter passing mechanism, the values printed by the above program are :
(A) 115, 220
(B) 25, 220
(C) 25, 15
(D) 115, 105


Answer: (A)

Explanation:

filter_none

edit
close

play_arrow

link
brightness_4
code

global int i = 100, j = 5;
void P(x) // x = i + j
{
    int i = 10;
    print(x + 10);// print (100+5+10) = 115
    i = 200;
    j = 20;
    print(x); // print (200+20) = 220. 
    // i and j would be changed as they are global variables
}
main()
{
    P(i + j);
}

chevron_right


Thus, A is the correct option as 115 and 220 are printed.

 
Please comment below if you find anything wrong in the above post.

Quiz of this Question



My Personal Notes arrow_drop_up


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.