GATE 2008
185 questions
MCQ 1851-mark 52 · 2-mark 133
Questions 81–85 of 185
#include <stdio.h>
int main ()
{
char a [6] = "world";
int i, j;
for (i = 0, j = 5; i < j; a [i++] = a [j--]);
printf ("%s\n", a);
} Answer: (B)
In the first pass, and , so , the null character. Now the first character of the array is the null character.
The loop goes on for two more passes ( and ) and stops when fails, but these do not change .
printf with %s prints characters from the start of the array until it meets a null character. The very first character is null, so it prints nothing: an empty (null) string.
Answer: (B)
1. f(int Y[10], int x) {
2. int i, j, k;
3. i = 0; j = 9;
4. do {
5. k = (i + j) /2;
6. if( Y[k] < x) i = k; else j = k;
7. } while(Y[k] != x && i < j);
8. if(Y[k] == x) printf ("x is in the array ") ;
9. else printf (" x is not in the array ") ;
10. } The correction needed in the program to make it work properly isAnswer: (A)
The correct choice is A.
if( Y[k] < x) then i = k + 1;
When the element we are looking for is greater than Y[k], the search goes on in the upper half of the array.
If not, we set ;
and the search goes on in the lower half.
Why the program as given fails: with and , the range stops shrinking once . For example, with and , the loop reaches and then computes for ever. Option (A) moves or past , so the range shrinks on every pass and the loop always ends.
(B) moves the bounds the wrong way. (C) keeps , so it can still loop for ever in the same way. (D) reverses the test, so the loop stops after the first pass unless .
Note: even with (A), the test in line 7 ends the loop when without looking at . With , the values are then reported as not in the array. A complete fix also needs in line 7. Of the four choices, only (A) fixes the real fault, so it is the intended answer.
Answer: (A)
1. f(int Y[10], int x) {
2. int i, j, k;
3. i = 0; j = 9;
4. do {
5. k = (i + j) /2;
6. if( Y[k] < x) i = k; else j = k;
7. } while(Y[k] != x && i < j);
8. if(Y[k] == x) printf ("x is in the array ") ;
9. else printf (" x is not in the array ") ;
10. } On which of the following contents of Y and x does the program fail?Answer: (C)
(C) is all s and . Always , so each time. Start with :
, so becomes . Now and again, so stays forever. Both and stay true, so this is an infinite loop and the program fails.
(A) and . For , and is found. For small , such as , moves down () and is found. The search works.
(B) All of is greater than when , so each time: . Then , the loop ends, , and it correctly prints that x is not in the array.
(D) For with , the search finds . For example, is found at , and at after .
Answer: (C)
#include <stdio.h>
# define swap1 (a, b) tmp = a; a = b; b = tmp
void swap2 ( int a, int b)
{
int tmp;
tmp = a; a = b; b = tmp;
}
void swap3 (int*a, int*b)
{
int tmp;
tmp = *a; *a = *b; *b = tmp;
}
int main ()
{
int num1 = 5, num2 = 4, tmp;
if (num1 < num2) {swap1 (num1, num2);}
if (num1 < num2) {swap2 (num1 + 1, num2);}
if (num1 >= num2) {swap3 (&num1, &num2);}
printf ("%d, %d", num1, num2);
} Answer: (C)
The paper prints a space between swap1 and (a, b). Strictly, that makes swap1 an object-like macro and the line with swap1 (num1, num2) would not compile; the question means the function-like macro swap1(a, b), so read it that way. That branch is skipped anyway.
Start with num1 = 5 and num2 = 4.
if (num1 < num2)for swap1: is false, so it is skipped.if (num1 < num2)for swap2: again false, so it is skipped. (swap2 would only swap copies anyway.)if (num1 >= num2): is true, so swap3(&num1, &num2) runs. It swaps through pointers, so num1 and num2 are really exchanged.
After the swap, num1 = 4 and num2 = 5. The program prints .
Answer: (C)
int f(int x, int *py, int **ppz)
{
int y, z;
**ppz += 1; z = **ppz;
*py += 2; y = *py;
x += 3;
return x+y+z;
}
void main()
{
int c, *b, **a;
c = 4; b = &c; a = &b;
printf("%d", f(c, b, a));
}Answer: (B)
In main, , b points to c, and a points to b. The call f(c, b, a) passes x as a copy of c, py as the address of c, and ppz as the address of b.
- changes c (through b) to 5, so .
- changes c to 7, so .
- changes only the copy: .
The function returns .
Answer: (B)