GATE 2006
191 questions
MCQ 1911-mark 191
Questions 91–95 of 191
int func(int m, int n)
{
if (E) return 1;
else return(func(m -1, n) + func(m - 1, n - 1));
} In the above function, which of the following is the correct expression for E?Answer: (C)
(A) is wrong: it stops at but not at . For the call func(2, 2) does not stop, so it returns func(1, 2) + func(1, 1) = 1 + 1 = 2 instead of 1, and func(3, 2) comes out as 4 instead of 3.
(B) and (D) use &&, so both conditions must hold together. Then a call such as func(2, 0) does not stop and the recursion goes on with n below 0 and never ends.
Answer: (C)
#include <stdio.h>
void swap (int *x, int *y)
{
static int *temp;
temp = x;
x = y;
y = temp;
}
void printab ()
{
static int i, a = -3, b = -6;
i = 0;
while (i <= 4)
{
if ((i++)%2 == 1) continue;
a = a + i;
b = b + i;
}
swap (&a, &b);
printf("a = %d, b = %d\n", a, b);
}
main()
{
printab();
printab();
} Answer: (D)
Trace the loop in printab. The test
(i++)%2 uses the old value of i, but the increment happens at once, so the statements after it see the new i. After i = 0:- old : even, i becomes , so ,
- old : odd, i becomes ,
continue (nothing added)- old : even, i becomes , add
- old : odd, i becomes ,
continue- old : even, i becomes , add
- : the loop ends
So each call adds to both a and b.
First call: and .
a and b are static, so they keep their values between calls. Second call: and .
The output is a = 6, b = 3 and then a = 15, b = 12.
Answer: (D)
subroutine swap(ix,iy)
it = ix
L1 : ix = iy
L2 : iy = it
end
ia = 3
ib = 8
call swap (ia, ib+5)
print *, ia, ib
end S1: The complier will generate code to allocate a temporary nameless cell, initialize it to 13, and pass the address of the cell to swap S2: On execution the code will generate a runtime error on line L1
S3: On execution the code will generate a runtime error on line L2
S4: The program will print 13 and 8
S5: The program will print 13 and -2
Exactly the following set of statement (s) is correct:
Answer: (B)
Inside swap, ix is ia and iy is the temporary cell. Trace:
L1: sets ia to 13.
L2: writes 3 into the temporary cell.
Both writes go to valid cells, so there is no runtime error at L1 or L2. S2 and S3 are false.
ib was never passed, only a copy of ib+5 in the temporary, so ib stays 8. The program prints 13 and 8. S4 is true. S5 (13 and -2) is false.
Answer: (B)
#include <stdio.h>
int a1[] = {6, 7, 8, 18, 34, 67};
int a2[] = {23, 56, 28, 29};
int a3[] = {-12, 27, -31};
int *x[] = {a1, a2, a3};
void print(int *a[])
{
printf("%d,", a[0][2]);
printf("%d,", *a[2]);
printf("%d,", *++a[0]);
printf("%d,", *(++a)[0]);
printf("%d\n", a[-1][+1]);
}
main()
{
print(x);
} Answer: (A)
1. a[0][2] = a1[2] = 8.
2. [] binds tighter than *, so *a[2] = *(a[2]) = *a3 = a3[0] = -12.
3. ++a[0] increases the pointer a[0] (that is x[0]) by one int, so it now points to a1[1]. The * gives 7. (a1 itself is an array name and cannot change; x[0] is what moves.)
4. In (++a)[0], the ++a runs first and a now points to x[1]. So (++a)[0] = x[1] = a2, and * gives a2[0] = 23.
5. a now points to x[1], so a[-1] is x[0], which points to a1[1] after step 3. Then [+1] moves one more place to a1[2] = 8.
Output: 8, -12, 7, 23, 8. (B) has 8 as the second value but *a[2] is -12; (C) and (D) start with -12 but a[0][2] is 8.
Answer: (A)
#include <stdio.h>
struct test {
int i;
char *c;
}st[] = {5, "become", 4, "better", 6, "jungle", 8, "ancestor", 7, "brother"};
main ()
{
struct test *p = st;
p += 1;
++p -> c;
printf("%s,", p++ -> c);
printf("%c,", *++p -> c);
printf("%d,", p[0].i);
printf("%s \n", p -> c);
} Answer: (B)
Program with the grouping of each expression marked:
#include <stdio.h>
struct test {
int i;
char *c;
}st[] = {5, "become", 4, "better", 6, "jungle", 8, "ancestor", 7, "brother"};
int main () {
struct test *p = st;
p += 1;
++p->c; // ++(p->c)
printf("%s,", p++->c); // (p++)->c
printf("%c,", *++p->c); // *(++(p->c))
printf("%d,", p[0].i);
printf("%s \n", p->c);
}
Assume an int takes bytes and a pointer takes bytes. With no padding between the members, each structure takes bytes.
Precedence: -> binds tighter than prefix ++ and unary *, as the figure shows. So ++p->c is ++(p->c) and *++p->c is *(++(p->c)). Postfix ++ is at the same level as -> and groups left to right, so p++->c is (p++)->c.
Starting state:
struct test *p = st; makes p point to st[0].
p += 1; adds bytes (one structure), so p points to st[1].
++p->c; moves the string pointer of st[1] on by one character, from "better" to "etter". p itself does not move.
printf("%s,", p++->c); prints "etter", and then p moves on to st[2].
printf("%c,", *++p->c); moves the string pointer of st[2] from "jungle" to "ungle", and prints its first character, 'u'.
printf("%d,", p[0].i); prints the integer of st[2], which is . In the figure, the form *(p+0)->i is not valid C. It should read (p+0)->i, which is the same as p[0].i.
printf("%s \n", p->c); prints the string now held by st[2], which is "ungle".
The output is etter,u,6,ungle. Options (A) and (C) are wrong because the first item is "etter", not "jungle" or "cetter". Option (D) is wrong because p is still on st[2] at the end, so the last two items are and "ungle", not and "ncestor".
Answer: (B)