GATE 2008
185 questions
MCQ 1851-mark 52 · 2-mark 133
Questions 76–80 of 185
void f (int n)
{
if (n <=1) {
printf ("%d", n);
}
else {
f (n/2);
printf ("%d", n%2);
}
} What does f(173) print?
Answer: (D)
The call f(n) first calls f(n/2), and only after it returns prints . So the bits of n print from the most significant bit to the least significant bit. When it just prints n.
The chain of calls is . The call f(1) prints 1. Then, as the calls return, they print for , which is .
The output is . This is the binary form of 173, since .
Answer: (D)
int f1(int n)
{
if(n == 0 || n == 1)
return n;
else
return (2*f1(n-1) + 3*f1(n-2));
}
int f2(int n)
{
int i;
int X[N], Y[N], Z[N] ;
X[0] = Y[0] = Z[0] = 0;
X[1] = 1; Y[1] = 2; Z[1] = 3;
for(i = 2; i <= n; i++)
{
X[i] = Y[i-1] + Z[i-2];
Y[i] = 2*X[i];
Z[i] = 3*X[i];
}
return X[n] ;
} f1(8) and f2(8) return the valuesAnswer: (C)
f1 uses the recurrence with .
In f2, the starting values are , and they satisfy and for (, , ). The loop keeps this true for every , because and . So and , and
.
This is the same recurrence with the same starting values, so f1 and f2 return the same value. Solve it:
The characteristic equation is , so and . The general solution is
Use and : and . So and . Put these in (1):
, so .
The same by hand:
Answer: (C)
void reverse(void) {
int c;
if (?1) reverse ();
?2
}
main() {
printf("Enter Text "); printf("\n") ;
reverse(); printf("\n");
}Answer: (D)
reverse has its own local variable c. A call first reads one character into c, and calls itself if that character is not the newline. Only after the inner call returns does it print its own c. So the characters are printed in the opposite order to how they were read.This needs ?1 to read and test in one step:
((c = getchar()) != '\n'). The brackets around c = getchar() are needed because = has lower priority than !=. Without them, c would get the result of the comparison (0 or 1).?2 must print c:
putchar(c). For input abc, the calls read a, b, c and the newline. The last call stops and prints the newline, then the earlier calls print c, b, a as they return.Why the others fail:
(A) reads a character but never stores it in c, and
getchar(c) is not a valid call (getchar takes no argument and prints nothing).(B) also uses
getchar(c) for ?2, which prints nothing.(C) tests c before anything has been read into it, and nothing reads input.
Answer: (D)
#include <stdio.h>
int main ()
{
int i, j;
char a [2] [3] = {{'a', 'b', 'c'}, {'d', 'e', 'f'}};
char b [3] [2];
char *p = *b;
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
*(p + 2*j + i) = a [i] [j];
}
}
} What should be the contents of the array b at the end of the program?
Answer: (B)
The statement therefore does b[j][i] = a[i][j]: it copies a into b transposed.
For i = 0 it writes offsets 0, 2, 4 with a[0][0..2] = a, b, c. For i = 1 it writes offsets 1, 3, 5 with a[1][0..2] = d, e, f.
Memory in order is a d b e c f, so the rows of b are (a d), (b e), (c f).
Option (A) would be a plain row-by-row copy, which needs *(p + 3*i + j); it does not match this index.
Answer: (B)
#include <stdio.h>
int main ()
{
int i, j;
int a [8] = {1, 2, 3, 4, 5, 6, 7, 8};
for(i = 0; i < 3; i++) {
a[i] = a[i] + 1;
i++;
}
i--;
for (j = 7; j > 4; j--) {
int i = j/2;
a[i] = a[i] - 1;
}
printf ("%d, %d", i, a[i]);
} Answer: (C)
: ; body makes ; loop makes .
: ; body makes ; loop makes .
: is false, so the loop ends. Then makes the outer .
Now .
Second loop: the statement declares a new inside the loop body. It hides the outer only inside that body, and the outer stays .
: inner , .
: inner , .
: inner , .
: is false, so the loop ends.
printf sees the outer and , so it prints .
Answer: (C)