Questions & Answers of Programming in C

Consider the following C program

#include <stdio.h>
#include <string.h>
 
void printlength (char *s, char *t) {
   unsigned int c=0;
   int len = ((strlen(s) - strlen(t)) > c) ? strlen(s): strlen(t);
   printf("%d\n",len);
}
 
void main () {
   char *x = "abc",
   char *y = "defgh";
   printlength (x,y);
}
Rcall that strlen is defined in string. h as returning a value of type size_t, which is an unsigned int. The output of the program is ______.

The output of executing the following C program is ____________ .

#include <stdio.h>
 
int total (int v) {
   static int count = 0;
   while(v) {
      count += v&1;
      v >>= 1;
   }
   return count ;
}
 
void main () {
    static int x = 0;
    int i =5;
    for (; i>0;i--) {
        x=x+total(i);
    }
    printf("%d\n",x);
}

Match the following:

(P) static char var,

(i) sequence of memory location to store addresses

(Q)m=malloc(10);
   m=NULL;
(ii) A variable located in data section of memory
(R) char *ptr[10];

(iii) Request to allocate a CPU register to store data

(s)register int varl;

(iv) A lost memory which cannot be free

 

Consider the following function implemented in C:
void printxy(int x, int y) {
    int *ptr;
    x=0;
    ptr=&x;
    y=*ptr;
    *ptr=1;
    printf(“%d, %d”, x, y);
}
The output of invoking printxy(1,1) is

Consider the C program fragment below which is meant to divide x by y using repeated subtraction. The variables x, y, q and r are all unsigned int.

while (r >=y) {
  r = r - y;
  q = q + 1;
}
Which of the  following condition on the variables x, y, q and r before the execution of the fragment will ensure that the loop terminates in a state satisfying the condition x == (y*q + r)?

Consider the following snippet of a C program. Assume that swap(&x, &y) exchanges the contents of x and y.

int main () {
      int array[] = {3,5,1,4,6,2};
      int done = 0;
      int i;
 
      while  (done == 0) {
             done = 1;
             for (i = 0, i<=4; i++) {
                   if (array[i]< array[i+1]) {
                        swap (&array[i], &array[i+1]);
                        done = 0;
                   }   
             }
             for (i=5; i>=1; i--) {
                  if (array[i] > array [i-1]) {
                       swap(&array[i], &array[i-1]);
                       done = 0;
                  }
             }
             printf("%d", array[3]);
}
The output of the program is ________. 

Consider the following C program.

#include<stdio.h>
int main () {
          int m = 10;
          int n, nl;
          n = ++m;
          n1 = m++;
          n--;
          --n1;
          n-= n1;
          printf ("%d",n),
          return 0;
}
The output of the program is ___________.

Consider the folowing C program.

#include<stidio.h>
#include<string.h>
int main () {
         char* c = "GATECSIT2017";
         char* p = c;
         printf ("%d", (int)strlen (c+2[p]-6[p]-1));
         return 0;
}
The output of the program is ____________.

Consider the following C program.

void f(int, short);
void main()
{
    int i = 100;
    short s = 12;
   short *p = &s;
   __________ ; // call to f()
}

Which one of the following expressions, when placed in the blank above, will NOT result in a type checking error?

The following function computes the maximum value contained in an integer array p[] of size

n (n >= 1).

   int max(int *p, int n) {
     int a=0, b=n-1;

     while (__________) {
       if (p[a] <= p[b]) { a = a+1; }
       else { b = b-1; }
     }
     return p[a];
   }

The missing loop condition is

What will be the output of the following C program?

void count(int n){
  static int d=1;
      printf("%d ", n);
      printf("%d ", d);
      d++;
      if(n>1) count(n-1);
      printf("%d ", d);
   }

   void main(){ 
      count(3);
   }

What will be the output of the following pseudo-code when parameters are passed by reference and dynamic scoping is assumed?

     a=3;
     void n(x) {x = x * a; print(x);}
     void m(y) {a = 1; a = y - a; n(a); print(a);}
    void main() {m(a);}

The value printed by the following program is __________.


    void f(int* p, int m){
          m = m + 5;
          *p = *p + m;
          return;
   }

    void main(){
          int i=5, j=10;

          f(&i, j);
          printf("%d", i+j);
    }

The output of the following C program is ______.
void f1(int a, int b)
{
    int c;
    c=a; a=b; b=c;
}
void f2(int *a, int *b)
{
   int c;
   c=*a; *a=*b; *b=c;
}
int main ( )
{
   int a=4, b=5, c=6;
   f1(a, b);
   f2(&b, &c);
   printf(“%d”, c–a–b);
}

What is the output of the following C code? Assume that the address of x is 2000 (in decimal) and an integer requires four bytes of memory?
int main( )
{
    unsigened int x[4][3] =
    {(1,2,3), {4,5,6}, {7, 8, 9}, {10, 11, 12}};
    printf(“%u, %u, %u”, x+3, *(x+3),*(x+2)+3);
}

Consider the following C program:

# include<stdio.h>
int main ( )
{
    int i, j, k = 0;
    j = 2 * 3 / 4 + 2.0 / 5 + 8 / 5;
    k –= – –j;
    for (i = 0; i < 5: i++)
    {
       switch (i + k)
       {
          case 1:
          case 2: printf (“\ n%d”, i+k);
          case 3: printf (“\n%d”, i+k);
          default: printf (“\n%d”, i+k);
       }
    }
    return 0;
}

The number of time printf statement is executed is ______.

Consider the following program in C language:

#include <stdio.h>
main()
{
int i;
int *pi = &i;
scanf(“%d”,pi);
printf(“%d\n”, i+5);
}

Which one of the following statements is TRUE?

Consider the function func shown below:

int func(int num) {
int count = 0;
    while (num) {
        count++;
        num>>= 1;
    }
return (count);
}

The value returned by func(435) is __________.

Suppose n and p are unsigned int variables in a C program. We wish to set p to nC3.If n is large, which one of the following statements is most likely to set p correctly?

Which one of the following is NOT performed during compilation?

Consider the following function

double f(double x){
     if( abs(x*x – 3) < 0.01) return x;
     else return f(x/2 + 1.5/x);
}

Give a value q (to 2 decimals) such that f(q) will return q:_____.

Consider the C function given below.

int f(int j)
{
     static int i = 50;
    int k;
        if (i == j)
        {
            printf(“something”);
            k = f(i);
            return 0;
        }
    else return 0;
}

Which one of the following is TRUE?

Let A be a square matrix of size n X n . Consider the following pseudocode. what is the exepected output?

c=100;

for i=1 to n do

    for j=1 to n do

        {

            Temp=A[i][j]+c;

            A[i][j]=A[j][i];

            A[j][i]=Temp-c;

        }

for i=1 to n do

    for j=1 to n do

        output(A[i][j]);

Let A be a square matrix of size n X n . Consider the following pseudocode. what is the exepected output?

c=100;

for i=1 to n do

    for j=1 to n do

        {

            Temp=A[i][j]+c;

            A[i][j]=A[j][i];

            A[j][i]=Temp-c;

        }

for i=1 to n do

    for j=1 to n do

        output(A[i][j]);

Which of the following statements are CORRECT?

1) Static allocation of all data areas by a compiler makes it impossible to implement recursion.
2) Automatic garbage collection is essential to implement recursion.
3) Dynamic allocation of activation records is essential to implement recursion.
4) Both heap and stack are essential to implement recursion.

What will be the output of the following C program segment?

char inChar = ‘A’ ;
switch ( inChar ) {
case ‘A’ : printf (“Choice A\ n”) ;
case ‘B’ :
case ‘C’ : printf (“Choice B”) ;
case ‘D’ :
case ‘E’ :
default : printf ( “ No Choice” ) ; }

Consider the following C code segment.

int a, b, c = 0;
void prtFun(void);
main( )
{  static int a = 1;      /* Line 1 */
   prtFun( );
   a += 1;
   prtFun( );
   printf(“ \n %d %d ”, a, b);
}
void prtFun(void)
{  static int a = 2;      /* Line 2 */
   int b = 1;
   a += ++b;
   printf(“ \n %d %d ”, a, b);
}

What output will be generated by the given code segment?

Consider the following C code segment.

int a, b, c = 0;
void prtFun(void);
main( )
{  static int a = 1;      /* Line 1 */
   prtFun( );
   a += 1;
   prtFun( );
   printf(“ \n %d %d ”, a, b);
}
void prtFun(void)
{  static int a = 2;      /* Line 2 */
   int b = 1;
   a += ++b;
   printf(“ \n %d %d ”, a, b);
}

What output will be generated by the given code segment if:
Line 1 is replaced by auto int a = 1;
Line 2 is replaced by register int a = 2;

What does the following fragment of C program print?

Char c [] = “GATE2011";
char *p = c;
printf ("%S", p + p[3] — p[1] );

Consider the following recursive C function that takes two arguments.
unsigned int foo (unsigned int n, unsigned int r) {
if (n>O) return ((n% r) + foo (n/r, r) );
else return 0;
}

What is the return value of the function foo when it is called as foo(345,10)?

Consider the following recursive C function that takes two arguments.
unsigned int foo (unsigned int n, unsigned int r) {
if (n>O) return ((n% r) + foo (n/r, r) );
else return 0;
}

What is the return value of the function foo when it is called as a foo(513,2)?

What does the following program print?

#include <stdio.h>
void f (int *p, int *q) {
p = q;
*p = 2;
}
int i=0, j=1;
int main ( ){
f(&i, &j);
printf("%d %d\n", i, j ;
return 0;

What is the value printed by the following C program?
#include <stdio.h>
int f(int * a, int n)
{
       if (n<= 0)return 0;
       else if(*a% 2= = 0) return * a + f(a +1,n -1);
       else return *a - f(a+1, n-1);
}
int main ( )
{
       int a[ ] = {12, 7, 13, 4, 11, 6};
       printf ("%d", f(a,6));
       return 0;
}

Consider the program below:

#include <stdio.h>
int fun(int n,int *f_p) {
   int t, f;
   if (n <= 1) {
      *f_p =1;
      return 1;
   }
   t = fun (n- 1, f_p);
   f = t + *f_p;
   *f_p = t;
   return f;
}
int main() {
   int x = 15;
   printf ("%d\ n", fun(5,&x));
   return 0;
}

The value printed is:

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)

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));

}

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 character.

void recerse void {
int c;
if (?1) reverse ();
?2
}
main () {
print f ("Enter Text"); print f ("\ n"); reverse (); print f ("\ n");
}

Consider the following C program that attempts to locate an element x in an array Y[ ] using binary search

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) print f ("x is in the array");
9.      else print f ("x is not in the array");
10. }

The correction needed in the program to make it work properly is