C-Programming |Gate-2008| previous year questions| Set-12

Set-15 C-Programming Gate 2005

C-Programming |Gate-2008|

1. Which combination of the integer variables x, y and z makes the variable a get the value 4 in the following expression? a = (x > y)? ((x > z)? x : z): ((y > z)? y : z) [GATE – 2008]

a. x = 3, y = 4, z = 2
b. x = 6, y = 5, z = 3
c. x = 6, y = 3, z = 5
d. x = 5, y = 4, z = 5

Answer : a)


2. What is printed by the following C program?
int f (int x, int * py, int * *ppz)
void main ( )  
{   
   { int y,    z;
  int   c,   * b,      * *a; * *ppz  +  = 1;  z = *ppz;
c = 4;     b = &c;  a = &b; * py  + = 2;   y = *py;
pr int f (” %d”, f (c, b, a)); x  + = 3; 
}
return x + y + z;
} [GATE – 2008]

a. 18
b. 19
c. 20
d. 21

Answer : b)


3. Choose the correct option to fill ? 1 and ? 2 so that the program below prints an input string in reverse order. Assume that the input string is terminated by a newline void recerse (void){ int c; if (?1)reverse ( ); ? 2 } main ( ) { pr int f (“Enter Text “); pr int f (“\ n “); reverse ( );pr int f (“\ n “); } [GATE – 2008]

a. ?1 is (getchar( ) != ’\n’)
?2 is getchar(c);

b. ?1 is (c = getchar( ) ) != ’\n’)
?2 is getchar(c);

c. ?1 is (c != ’\n’)
?2 is putchar(c);

d. ?1 is ((c = getchar()) != ’\n’)
?2 is putchar(c);

Answer : d)


4. Match the programming paradigms and languages given in the following table. [GATE – 2008]

 Paradigms Languages
I)Imperativea)Prolog
II)Object orientedb)Lisp
III)Functionalc)C,Fortran 77, Pascal
IV)Logicd)C++ , Smalltalk, Java

a. I-c, II-d, III-b, IV-a
b. I-a, II-d, III-c, IV-b
c. I-d, II-c, III-b, IV-a
d. I-c, II-d, III-a, IV-b

Answer : a)


5. What is the output printed by the following C code? [GATE – 2008]

# include <stdio.h>
int main ()
{
    char a [6] = "world";
    int i, j;
    for (i = 0, j = 5; i < j; a [i++] = a [j--]);
    printf ("%sn", a);
}
 /* Add code here. Remove these lines if not writing code */

a. Dlrow
b. Null String
c. Dlrld
d. worow

Answer : b)


6. Consider the C program below. What does it print? [GATE – 2008]

# include <stdio.h>
# define swapl (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);
}
 /* Add code here. Remove these lines if not writing code */

a. 5,5
b. 5,4
c. 4,5
d. 4,4

Answer : c)


7. Consider the C program given below. What does it print? [GATE – 2008]

#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]);
}
 /* Add code here. Remove these lines if not writing code */

a. 2,3
b. 2,4
c. 3,2
d. 3,3

Answer : c)


8. C program is given below:

# 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];
              }
        }
}
 /* Add code here. Remove these lines if not writing code */

What should be the contents of the array b at the end of the program? [GATE – 2008]

a. ab
cd
ef

b. ad
be
cf

c. ac
eb
df

d. ae
dc
bf

Answer : b)


9. Consider the code fragment written in C below :

void f (int n) {   if (n <=1)  {    printf ("%d", n);   }   else {    f (n/2);    printf ("%d", n%2);   } }

What does f(173) print? [GATE – 2008]

a. 010110101
b. 010101101
c. 10110101
d. 10101101

Answer : d)


10. Consider the code fragment written in C below : [GATE – 2008]

void f (int n)
{
    if (n <= 1) 
{  
       printf ("%d", n);
    }  
   else 
{
        f (n/2);  
       printf ("%d", n%2);
    }
}

Which of the following implementations will produce the same output for f(173) as the above code? P1

void f (int n)
{
    if (n/2)
  {
        f(n/2);  
   }  
   printf ("%d", n%2);
}

P2

void f (int n)
{
    if (n <=1) 
{
        printf ("%d", n);
    }
    else 
{
        printf ("%d", n%2);
        f (n/2);  
   }
}

a. Both P1 and P2
b. P2 only
c. P1 only
d. Neither P1 nor P2

Answer : c)
C-Programming |Gate-2008|


Back to GATE-HOME


Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *