Array and Pointer: GATE CS Previous Year Questions

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

2000Questions per year2026

Questions 36–40 of 54

Q36GATE 20082 marksMCQ−0.67 if wrong
Consider the following C program that attempts to locate an element x in an array Y[] using binary search. The program is erroneous.
 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 is

Q37GATE 20082 marksMCQ−0.67 if wrong
Consider the following C program that attempts to locate an element x in an array Y[] using binary search. The program is erroneous.
 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?

Q38GATE 20082 marksMCQ−0.67 if wrong
Consider the C program below. What does it print?
 #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);
}

Q39GATE 20082 marksMCQ−0.67 if wrong
What is printed by the following C program?
 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));
    	
}

Q40GATE 20071 markMCQ−0.33 if wrong
Consider the C program given below :
 #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?

Keep practising

Topics in Programming in C

All CS topics ›