Monday, 22 December 2014

Interview Questions in C Programming

//1. What does the following C Program do?
main()
{
      unsigned int num;
      int i;
      scanf("%u",&num);
      for(i=0;i<16;i++)
      printf("%d",(num<<i & 1<<15)?1:0);
}
/*    a. It prints all even bits from num
      b. It prints all odd bits from num
      c. It prints binary equivalent of num
      d. It gives compilation error
      e. None of these
*/

//2. What will be the output of the following program?
#include<stdio.h>
main()
{
      struct xx
      {
            int x;
            char s;
      };
      struct xx *t;
      t->x=5;
      t->s='a';
      printf(%d %c",t->x,t->s);
}

/* Ans: _____________________ */

//3. Point out if there is any error
#include<studio.h>
#define decode(s,t,u,m,p,e,d)m##s##u##t
#define begin decode (a,n,i,m,a,t,e)
int begin()
{
      printf("hello");
}

/* Ans: _____________________*/

//4. Consider the declaration
      static struct {
                  unsigned a:5;
                  unsigned b:5;
                  unsigned c:5;
                  unsigned d:%;
            }v = (1,2,3,4);
           
/* V occupies:
      a. 4 words
      b. 2 words
      c. 1 word
      d. 3 words
      e. none of these
*/

//5. What is the output of the following C Program?
main(){
      printf("%d%d%d",sizeof(3.14f),sizeof(3.14),sizeof(3.14l));
}

//6.
main ()
{
      int i = 300;
      char *ptr = &i;
      *++ptr = 2;
      printf("%d",i);
}
/*    a. 556
      b. 56
      c. 554
      d. Segmentation fault
      e. None of these
*/

//7. Consider the following C function

Int(int n)
{
      static int r = 0;
      if(n<=0) return 1;
      if(n>3)
      {
            r = n;
            return f(n-2)+2;
      }
      return f(n-1)+r;
}

/* What is the value of f(5)?
a. 5 b. 7 c. 9 d. 18 e. None of these
*/

//8.
main()
{
      int x,y,m,n;
      scanf("%d %d",&x,&y);
      /*Assume x>0 and y>0*/
      m=x;n=y;
      while(m!=n)
      {
            if(m>n)
                  m=m-n;
            else
                  n=n-m;
      }
      printf("%d",n);
}

/* The program computes
      a. x+y, using repeated subtraction
      b. x mod y using repeated subtraction
      c. the greatest common divisor of x and y
      d. the least common multiple of x and y
      e. none of the above
*/


//9. Give output
#if something == 0
int some = 0;
#endif

main()
{
      int thing = 0;
      printf("%d %d\n",some,thing);
}

/* a. Compilation error b. Segmentation Fault c. 0 garbage value d. garbage value 0
*/

// 10. Give output
main()
{
      int i = 5, j = 6, z;
      printf(%d",i+++j);
}

/* a. 10 b. 11 c. 12 d. Vary from compiler to compiler e. Compilation error
*/

// 11. Give output
main()
{
      void swap)();
      int x=10,y=8;
      swap(&x,&y);
      printf("x=%d y=%d,x,y);
}
void swap(int *a, int * b)
{
      *a^=*b; *b^=*a; *a^=*b;
}

/*    a. x=10 y=8
      b. x=8 y=10
      c. x=10 y=10
      d. x=8 y=8
      e. Compilation Error
*/

//12. Give output
main()
{
      extern out;
      printf("%d",out++);
}
int out = 100;

/*a. Compilation Error b. Garbage Value c. 100 d. 101 e. None of these */

//13. Give output
main()
{
      int i, n;
      char *x="girl";
      n=strlen(x);
      *x=x[n];
      for(i=0;i<n;++i)
      {
            printf("%s\n",x);
            x++;
      }
}

/* a. lrig b. irl rl l c. girl irl rl l d. No output e. Compilation Error
*/

//14. Give output
main()
{
      int a=10,b;
      a>=5?b=100:b = 200;
      printf("\n%d",b);
}

/* a. 100 b. 200 c. No output d. Compilation Error e. None of these
*/

//15. Give output
main()
{
      char str1[]="Hello";
      char str2[]="Hello";
      if(str1==str2)
            printf("Equal");
      else
            printf("Unequal");
}

/* a. Equal b. Unequal c. Depends on the compiler d. Compilation Error e. None of these
*/

//16. Give output
main()
{
      char str[10]="Angel";
      str[6]='d';
      printf("%s",str);
}

/*    a. Angel d
      b. d
      c. Angel
      d. Compilation Error
      e. Segmentation Fault
*/

//17. Give output
main()
{
      int i = 5;
      printf("%d %d %d %d %d",i++,i--,++i,--i,i);
}

/*    a. 4 5 5 5 5
      b. 6 5 6 5 5
      c. 5 6 5 6 6
      d. 4 5 5 4 5
      e. 5 6 6 5 5
*/

//18. Give output
main()
{
      printf("%p",main();
}
/*    a. 0x0000
      b. No output
      c. Some Garbage value
      d. The program executes infinite times till stack doesn't overflow.
      e. Compilation Error
*/

//19. Give output
main()
{
      int i = 5;
      printf("%d",i+++++i);
}

/* a. 12 b. 13 c. 11 d. Vary from compiler to compiler e. None of these
*/

//20. Give output
main()
{
      int i = _|_abc(10);
      printf("%d\n"--i);
}
int _|_abc(int i)
{
      return(i++);
}

/*    a. 10
      b. 9
      c. Compilation Error
      d. Output is unpredictable
      e. None of these
*/

//21. Give output
main()
{
      static int i = 5;
      if(--i){
            main();
            printf("%d",i);
      }
}

/*    a. 0 0 0 0 0
      b. 5 4 3 2 1
      c. The program will execute till stack doesn't overflow.
      d. 4 3 2 1 0
      e. No Output

*/

Note to readers: If you find any error, please mail it to 27it08@gmail.com or comment in the post. Thanks.

*/

0 comments

 
© 2011-2012 ProgrammingBlue
Posts RSS Comments RSS
Back to top