GATE 2005
203 questions
MCQ 2031-mark 203
Questions 91–95 of 203
What is the output printed by the following program?
#include <stdio.h>
int f(int n, int k) {
if (n == 0) return 0;
else if (n % 2) return f(n/2, 2*k) + k;
else return f(n/2, 2*k) - k;
}
int main () {
printf("%d", f(20, 1));
return 0;
} Answer: (C)
The rule is: if n is odd, ; if n is even, ; and . In the figure each box shows a call written in terms of the next call below it, and the number in the small square on the arrow is the value that call returns to the one above.
, so going back up:
Answer: (C)
Consider the following C-program:
double foo (double); /* Line 1 */
int main()
{
double da, db;
// input da
db = foo(da);
}
double foo(double a)
{
return a;
} The above code compiled without any error or warning. If Line 1 is deleted, the above code will show:Answer: (D)
Without Line 1, foo is called before it is declared. Old C then assumes an implicit declaration , with return type int. The later definition has a different return type, so the compiler reports a conflicting-types error. Also, implicit declaration of functions is not allowed at all from C99 onwards.
Answer: (D)
Answer: (D)
Consider the following C-program:
void foo(int n, int sum)
{
int k = 0, j = 0;
if (n == 0) return;
k = n % 10;
j = n / 10;
sum = sum + k;
foo (j, sum);
printf ("%d,", k);
}
int main ()
{
int a = 2048, sum = 0;
foo (a, sum);
printf ("%d\n", sum);
getchar();
} What does the above program print?Answer: (D)
C passes
Going down:
foo(2048, 0): k = 8, j = 204, sum = 8; calls foo(204, 8).
foo(204, 8): k = 4, j = 20, sum = 12; calls foo(20, 12).
foo(20, 12): k = 0, j = 2, sum = 12; calls foo(2, 12).
foo(2, 12): k = 2, j = 0, sum = 14; calls foo(0, 14).
foo(0, 14): n == 0, returns without printing.
Coming back up, the calls print their k in order: 2, then 0, then 4, then 8.
In main, sum was passed by value, so it is still 0, and main prints 0. The output is 2,0,4,8,0.
Answer: (D)
n and sum by value, so each call works on its own copies. The print of k comes after the recursive call, so it runs as the calls return.Going down:
foo(2048, 0): k = 8, j = 204, sum = 8; calls foo(204, 8).
foo(204, 8): k = 4, j = 20, sum = 12; calls foo(20, 12).
foo(20, 12): k = 0, j = 2, sum = 12; calls foo(2, 12).
foo(2, 12): k = 2, j = 0, sum = 14; calls foo(0, 14).
foo(0, 14): n == 0, returns without printing.
Coming back up, the calls print their k in order: 2, then 0, then 4, then 8.
In main, sum was passed by value, so it is still 0, and main prints 0. The output is 2,0,4,8,0.
Answer: (D)
What does the following C-statement declare?
int ( * f) (int * ) ; Answer: (C)
Start at the name and move outwards: (*f) tells us that f is a pointer. Next, (int *) tells us it points to a function that takes an int pointer as its argument. The int at the front is the return type of that function.
Answer: A pointer to a function that takes an integer pointer as argument and returns an integer
Answer: A pointer to a function that takes an integer pointer as argument and returns an integer
Let a be an array containing n integers in increasing order. The following algorithm determines whether there are two distinct numbers in the array whose difference is a specified number S > 0.
Choose the correct expression for E.
i = 0; j = 1;
while (j < n ){
if (E) j++;
else if (a[j] - a[i] == S) break;
else i++;
}
if (j < n) printf("yes") else printf ("no"); Choose the correct expression for E.
Answer: (B)
The array is in increasing order, so for fixed i the difference a[j] - a[i] grows as j grows, and for fixed j it shrinks as i grows. Compare the difference with S:
- If a[j] - a[i] < S, it is too small. Only a larger j can help, so j++. This is the test E.
- If a[j] - a[i] == S, the pair is found, so break.
- Otherwise the difference is larger than S, so a bigger j only makes it worse; move i up with i++.
Since S > 0, a[j] - a[i] is 0 when i = j, which is less than S, so i never gets ahead of j. The loop ends either with a break (j < n, prints yes) or when j reaches n (prints no).
(A) would do j++ when the difference is already too big. (C) is a[i] - a[j] < S, which is true for every j ≥ i (it is 0 or negative), so j would always increase and the pair would never be found. (D) is never true for j ≥ i.
Answer: (B)
- If a[j] - a[i] < S, it is too small. Only a larger j can help, so j++. This is the test E.
- If a[j] - a[i] == S, the pair is found, so break.
- Otherwise the difference is larger than S, so a bigger j only makes it worse; move i up with i++.
Since S > 0, a[j] - a[i] is 0 when i = j, which is less than S, so i never gets ahead of j. The loop ends either with a break (j < n, prints yes) or when j reaches n (prints no).
(A) would do j++ when the difference is already too big. (C) is a[i] - a[j] < S, which is true for every j ≥ i (it is 0 or negative), so j would always increase and the pair would never be found. (D) is never true for j ≥ i.
Answer: (B)