Array and Pointer · Programming in C
54 questions · asked in 22 of 27 years (2000–2026)
MCQ 39 · MSQ 1 · NAT 141-mark 34 · 2-mark 20
Questions 36–40 of 54
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)
#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)
Keep practising