GATE 2007
184 questions
MCQ 1841-mark 184
Questions 71–75 of 184
int i ;
program main ()
{
i = 10;
call f();
}
procedure f()
{
int i = 20;
call g ();
}
procedure g ()
{
print i;
} Let x be the value printed under static scoping and y be the value printed under dynamic scoping. Then, x and y are:
Answer: (A)
Under static scoping, a name is resolved by where it is written in the code. Procedure g has no local i, and it is written at the global level, so its i is the global i. The global i was set to 10 in main, so .
Under dynamic scoping, a name is resolved through the chain of calls. The chain is main, then f, then g. Procedure g has no local i, so the search goes to its caller f, which has a local . So g prints 20 and .
So and .
Answer: (A)
int i ;
program main ()
{
int j = 60;
i = 50;
call f (i, j);
print i, j;
}
procedure f (x, y)
{
i = 100;
x = 10;
y = y + i ;
} Which one of the following options represents the correct output of the program for the two parameter passing mechanisms?
Answer: (D)
Call by reference: x is another name for i, and y is another name for j. sets i to . sets x, which is the same location as i, so . gives , and this changes j too. Back in main, and .
Answer: (D)
int f (int n) {
if (n <= 1) return 1;
else if (n % 2 == 0) return f(n/2);
else return f(3n - 1);
} Assuming that arbitrarily large integers can be passed as a parameter to the function, consider the following statements.
i. The function f terminates for finitely many different values of .
ii. The function f terminates for infinitely many different values of .
iii. The function f does not terminate for finitely many different values of .
iv. The function f does not terminate for infinitely many different values of .
Which one of the following options is true of the above?
Answer: (D)
The paper writes f(3n - 1), which is not valid C; it means f(3*n - 1).
The function stops for every power of . There are infinitely many such numbers. So (i) is false and (ii) is TRUE.
Take
The recursive calls go like this:
This goes on as infinite recursion. Now multiply by any power of . The result is again infinite recursion. There are infinitely many powers of , so infinitely many inputs give infinite recursion (this case alone is enough). So (iv) is TRUE and (iii) is false.
Answer: (D)
#include <stdio.h>
int f(int n)
{
static int r = 0;
if (n <= 0) return 1;
if (n > 3)
{
r = n;
return f(n-2)+2;
}
return f(n-1)+r;
}
int main()
{
printf("%d", f(5));
} What is the value of f(5)?Answer: (D)
(since , r becomes 5)
(since )
Work back from the last call: , , , so .
Answer: (D)
#include <stdio.h>
int main () {
int sum = 0, maxsum = 0, i, n = 6;
int a [] = {2, -2, -1, 3, 4, 2};
for (i = 0; i < n; i++) {
if (i == 0 || a [i] < 0 || a [i] < a [i - 1]) {
if (sum > maxsum) maxsum = sum;
sum = (a [i] > 0) ? a [i] : 0;
}
else sum += a [i];
}
if (sum > maxsum) maxsum = sum ;
printf ("%d\n", maxsum);
} What is the value printed out when this program is executed?
Answer: (C)
After the loop, is false, so stays . The program prints .
Answer: (C)