Line 977: Line 977:
 
{
 
{
 
     char a[100] = {0};
 
     char a[100] = {0};
     printf("%lu %lu",sizeof(a),strlen(a));
+
     printf("%zu %zu",sizeof(a),strlen(a));
  
 
     return 0;
 
     return 0;
Line 984: Line 984:
 
|type="{}"
 
|type="{}"
 
/}
 
/}
{ 1 }
+
{ 100 0 }
  
  
  
 
 
{'''What is the output of the following program?'''
+
{'''What is the output of the following program?''' (Assume input is 10)
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
#include<stdio.h>
 
#include<stdio.h>

Revision as of 20:32, 19 May 2014

<quiz display="simple"> {What is the output of this program? <syntaxhighlight lang="c" >

  1. include<stdio.h>

int main() {

  1. char *ptr;
  2. char string[] = "How are you?";
  3. ptr = string;
  4. ptr += 4;
  5. printf("%s",ptr);
  6. return 0;

} </syntaxhighlight> |type="()" /} -(a) How are you?

+(b) are you?

-(c) are

-(d) No output


{ Which of the following will print the value 2 for the above code? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. int a[10][20][30] = {0};
  2. a[5][2][1] = 2;
  1. return 0;

} </syntaxhighlight> |type="()" /} -(a) printf("%d",*(((a+5)+2)+1));

-(b) printf("%d",***((a+5)+2)+1);

+(c) printf("%d",*(*(*(a+5)+2)+1));

-(d) None of these


{ What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. int a = 5;
  2. int b = ++a * a++;
  3. printf("%d ",b);
  4. return 0;

} </syntaxhighlight> |type="()" /} -(a)25

-(b) 30

-(c) 36

+(d) Undefined Behavior


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. int a = 5;
  2. switch(a)
  3. {
  4. default:
  5. a = 4;
  6. case 6:
  7. a--;
  8. case 5:
  9. a = a+1;
  10. case 1:
  11. a = a-1;
  12. }
  13. printf("%d \n",a);
  1. return 0;

} </syntaxhighlight> |type="()" /} +(a) 5

-(b) 4

-(c) 3

-(d) None of these


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. int a = 2,b = 5;
  2. a = a^b;
  3. b = b^a;
  4. printf("%d %d",a,b);
  1. return 0;

} </syntaxhighlight> |type="()" /} -(a) 5 2

-(b) 2 5

-(c) 7 7

+(d) 7 2


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

  1. int a[][3] = {1, 2, 3, 4, 5, 6};
  2. int (*ptr)[3] = a;
  3. printf("%d %d ", (*ptr)[1], (*ptr)[2]);
  4. ++ptr;
  5. printf("%d %d\n", (*ptr)[1], (*ptr)[2]);
  1. return 0;

} </syntaxhighlight> |type="()" /} +(a) 2 3 5 6

-(b) 2 3 4 5

-(c) 4 5 0 0

-(d) none of the above


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

void f(char**);

int main() {

  1. char *argv[] = { "ab", "cd", "ef", "gh", "ij", "kl" };
  2. f(argv);
  1. return 0;

} void f(char **p) {

  1. char *t;
  2. t = (p += sizeof(int))[-1];
  3. printf("%s\n", t);

} </syntaxhighlight> |type="()" /} -(a) ab

-(b) cd

-(c) ef

+(d) gh


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdarg.h>
  2. include <stdio.h>

int ripple(int n, ...) {

  1. int i, j, k;
  2. va_list p;
  3. k = 0;
  4. j = 0;
  5. va_start(p, n);
  6. for (; j < n; ++j)
  7. {
  8. i = va_arg(p, int);
  9. k += i;
  10. }
  11. va_end(p);
  12. return k;

} int main() {

  1. printf("%d\n", ripple(2, 5, 7));
  1. return 0;

} </syntaxhighlight> |type="()" /} +(a) 12

-(b) 5

-(c) 7

-(d) 15


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

int counter(int i) {

  1. static int count = 0;
  2. count = count + i;
  3. return count;

}

int main() {

  1. int i, j;
  2. for (i = 0; i <= 5; i++)
  3. j = counter(i);
  4. printf("%d\n", j);
  1. return 0;

} </syntaxhighlight> |type="()" /} -(a) 10

+(b) 15

-(c) 6

-(d) 7


{ What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. const int x = 5;
  2. const int *ptrx;
  3. ptrx = &x;
  4. *ptrx = 10;
  5. printf("%d\n", x);
  1. return 0;

} </syntaxhighlight> |type="()" /} -(a) 5

-(b) 10

+(c) Compile Error

-(d) Garbage value


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>
  2. define x 4+1

int main() {

  1. int i;
  2. i = x*x*x;
  3. printf("%d",i);
  1. return 0;

} </syntaxhighlight> |type="()" /} -(a) 125

+(b) 13

-(c) 17

-(d) None of above


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. char c=125;
  2. c=c+10;
  3. printf("%d",c);
  1. return 0;

} </syntaxhighlight> |type="()" /} -(a) 135

-(b) +INF

+(c) -121

-(d) -8


{ What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. int i=10;
  2. static int x=i;
  3. if(x==i)
  4. printf("Equal");
  5. else if(x>i)
  6. printf("Greater");
  7. else
  8. printf("Lesser");
  1. return 0;

} </syntaxhighlight> |type="()" /} -(a) Equal

-(b) Greater

-(c) Lesser

+(d) Compile Error


{Consider the following code segment: <syntaxhighlight lang="c">

  1. include <stdlib.h>

int *f1() {

  1. int x = 10;
  2. return &x;

} int *f2() {

  1. int *ptr;
  2. *ptr = 10;
  3. return ptr;

} int *f3() {

  1. int *ptr;
  2. ptr = (int*) malloc(sizeof (*ptr));
  3. return ptr;

} </syntaxhighlight> Which of these functions uses pointers incorrectly? |type="()" /} +(a) f3 only

-(b) f1 and f3

-(c) f1 and f2

-(d) f1, f2, and f3


{ What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

  1. int i = 3;
  2. int j;
  3. j = sizeof(++i + ++i);
  4. printf("i=%d j=%d\n", i, j);
  1. return 0;

} </syntaxhighlight> |type="()" /} -(a) i=4 j=4

+(b) i=3 j=4

-(c) i=5 j=4

-(d) the behavior is undefined


{ What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

void f1(int*, int); void f2(int*, int); void (*p[2])(int*, int);

int main() {

  1. int a = 3;
  2. int b = 5;
  3. p[0] = f1;
  4. p[1] = f2;
  5. p[0](&a, b);
  6. printf("%d %d ", a, b);
  7. p[1](&a, b);
  8. printf("%d %d\n", a, b);
  1. return 0;

}

void f1(int *p, int q) {

  1. int tmp = *p;
  2. *p = q;
  3. q = tmp;

} void f2(int *p, int q) {

  1. int tmp = *p;
  2. *p = q;
  3. q = tmp;

} </syntaxhighlight> |type="()" /} +(a) 5 5 5 5

-(b) 3 5 3 5

-(c) 5 3 3 5

-(d) none of the above


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

void e(int);

int main() {

  1. int a = 3;
  2. e(a);
  3. putchar('\n');
  1. return 0;

} void e(int n) {

  1. if (n > 0)
  2. {
  3. e(--n);
  4. printf("%d ", n);
  5. e(--n);
  6. }

} </syntaxhighlight> |type="()" /} +(a)0 1 2 0

-(b) 0 1 2 1

-(c) 1 2 0 1

-(d) 0 2 1 1


{ Consider the following code segment: <syntaxhighlight lang="c"> typedef int (*test)(float*, float*); test tmp; </syntaxhighlight> What is the type of tmp? |type="()"

/} -(a) function taking two pointer-to-float arguments and returning pointer to int

-(b) pointer to int

+(c) pointer to function taking two pointer-to-float arguments and returning int

-(d) none of the above


{ What is the output of the following program?' <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

  1. char p;
  2. char buf[10] = {1, 2, 3, 4, 5, 6, 9, 8};
  3. p = (buf + 1)[5];
  4. printf("%d\n", p);
  1. return 0;

} </syntaxhighlight> |type="()" /} -(a) 5

-(b) 6

+(c) 9

-(d) none of the above


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

  1. struct node
  2. {
  3. int a;
  4. int b;
  5. int c;
  6. };
  7. struct node s = { 3, 5, 6 };
  8. struct node *pt = &s;
  9. printf("%d\n", *((int*)pt+1));
  1. return 0;

} </syntaxhighlight> |type="()" /} -(a) 3

+(b) 5

-(c) 6

-(d) 7


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

int main(void) {

  1. char a[5] = { 1, 2, 3, 4, 5 };
  2. char *ptr = (char*)(&a + 1);
  3. printf("%d %d\n", *(a + 1), *(ptr - 1));
  1. return 0;

} </syntaxhighlight> |type="()" /} -(a) Compile Error

-(b) 2 1

+(c) 2 5

-(d) none of the above


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

void foo(int[][3]);

int main(void) {

  1. int a[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
  2. foo(a);
  3. printf("%d\n", a[2][1]);
  1. return 0;

}

void foo(int b[][3]) {

  1. ++b;
  2. b[1][1] = 9;

} </syntaxhighlight> |type="()" /} -(a) 8

+(b) 9

-(c) 7

-(d) none of the above


{Consider the following function: <syntaxhighlight lang="c"> int foo(int x, int n) {

  1. int val = 1;
  2. if (n > 0)
  3. {
  4. if (% 2 == 1)
  5. val *= x;
  6. val *= foo(x * x, n / 2);
  7. }
  8. return val;

} </syntaxhighlight>

What function of x and n is computed by foo? |type="()" /} +(a) $x^n$

-(b) $x×n$

-(c) $n^x$

-(d) none of the above

{What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. int a = 0;
  2. switch(a)
  3. {
  4. default:
  5. a = 4;
  6. case 6:
  7. a--;
  8. case 5:
  9. a = a+1;
  10. case 1:
  11. a = a-1;
  12. }
  13. printf("%d \n",a);
  1. return 0;

} </syntaxhighlight> |type="()" /} -(a) 5

-(b) 4

+(c) 3

-(d) 0


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. int a = 2;
  2. if(a == (1,2))
  3. printf("Hello");
  4. if(a == 1,2)
  5. printf("World");
  1. return 0;

} </syntaxhighlight> |type="()" /} -(a) Hello

-(b) World

+(c) HelloWorld

-(d) Compile Error


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. int a = 1,2;
  2. int b = (1,2);
  3. if(a == b)
  4. printf("Equal");
  5. else
  6. printf("Not Equal");
  1. return 0;

} </syntaxhighlight> |type="()" /} -(a) Equal

-(b) Not Equal

-(c) Compiler Dependent

+(d) Compile Error


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

void foo(char *);

int main() {

  1. char *string = "Hello";
  2. foo(string);
  3. printf("%s",string);
  1. return 0;

}

void foo(char *a) {

  1. while(*a)
  2. {
  3. *a += 1;
  4. a++;
  5. }

} </syntaxhighlight> |type="()" /} -(a) Hello

-(b) Ifmmp

-(c) Compile Error

+(d) Segmentation fault


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>
  2. include<stdlib.h>

int main() {

  1. char s[] = "Hello World";
  2. int i = 0;
  3. while(*(s++))
  4. i++;
  5. printf("%d",i);
  1. return 0;

} </syntaxhighlight> |type="()" /} -(a) Segmentation Fault

+(b) Compile Error

-(c) 12

-(d) 0


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int a = 10; int main() {

  1. fun();
  2. fun();
  1. return 0;

}

int fun() {

  1. static int a = 1;
  2. printf("%d ",a);
  3. a++;
  1. return 0;

} </syntaxhighlight> |type="()" /} +(a) 1 2

-(b) 1 1

-(c) 10 11

-(d) 10 10


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>
  2. define crypt(s,t,u,m,p,e,d) m##s##u##t
  3. define begin crypt(a,n,i,m,a,t,e)

int begin() {

  1. printf("Hello\n");
  1. return 0;

} </syntaxhighlight> |type="()" /} +(a) Hello

-(b) Link error

-(c) Segmentation fault

-(d) Compiler error


{Consider the following program: <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. int a[10][20][30]={0};
  2. printf("%ld",&a+1 - &a);
  1. return 0;

} </syntaxhighlight> What is the output of this program? |type="{}" /} { 1 }


{Consider the following program: <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. int a[10][20][30] = {0};
  2. int *b = a;
  3. int *c = a+1;
  4. printf("%ld", c-b);
  1. return 0;

} </syntaxhighlight> What is the output of this program? (You may ignore compiler warnings) |type="{}" /} { 600 }


{ What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>
  2. include<stdlib.h>

int* fun();

int main() {

  1. int *a = fun();
  2. printf("%d",*a);
  1. return 0;

} int* fun() {

  1. int *a =(int*) malloc(sizeof(int));
  2. *a = 10;
  3. return a;

} </syntaxhighlight> |type="{}" /} { 10 }


{ What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. int *a = fun();
  2. printf("%d",a);
  1. return 0;

} int fun() {

  1. int a = 10;
  2. return a;

} </syntaxhighlight> |type="{}" /} { 10 }


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>
  2. include<string.h>

int main() {

  1. char string[] = "Hello";
  2. printf("%zu %zu",sizeof(string),strlen(string));
  1. return 0;

} </syntaxhighlight> |type="{}" /} { 6 5 }


{ What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. float a = 0.5;
  2. if(a == 0.5)
  3. printf("Yes");
  4. else
  5. printf("No");
  1. return 0;

} </syntaxhighlight> |type="{}" /} { Yes }


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>
  2. include<string.h>

void foo(char *);

int main() {

  1. char a[100] = {0};
  2. printf("%zu %zu",sizeof(a),strlen(a));
  1. return 0;

} </syntaxhighlight> |type="{}" /} { 100 0 }



{What is the output of the following program? (Assume input is 10) <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. int a;
  2. printf("%d",scanf("%d",&a));
  1. return 0;

} </syntaxhighlight> |type="{}" /} { 1 }


{If the binary equivalent of 5.375 in normalised form is 0100 0000 1010 1100 0000 0000 0000 0000, what will be the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>
  2. include<math.h>

int main() {

  1. float a=5.375;
  2. char *p;
  3. int i;
  4. p = (char*)&a;
  5. for(i=0; i<2; i++)
  6. printf("%x ", (unsigned char)(p[i]^p[3-i]));
  1. return 0;

} </syntaxhighlight> |type="{}" /} { 40ac }


{ What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. char str[] = {'a','b','c','\0'};
  2. str[0] -= 32;
  3. printf("%s",str);
  1. return 0;

} </syntaxhighlight> |type="{}" /} { Abc }

</quiz>

Some codes

1. What is the following function doing? <syntaxhighlight lang="c"> int foo(int n) {

  1. int sum = 0;
  2. while(n > 0)
  3. {
  4. n = n & n-1;
  5. sum++;
  6. }
  7. return sum;

} </syntaxhighlight> Ans:


2. What is the following function doing? <syntaxhighlight lang="c"> int foo(int a, int b) {

  1. int c = a, d = b;
  2. while(!= b)
  3. {
  4. if(a < b)
  5. a = a+c;
  6. else
  7. b = b+d;
  8. }
  9. return a;

} </syntaxhighlight> Ans:


43. What is the following function doing? <syntaxhighlight lang="c"> int foo( int a, int b) {

  1. int c = a-b;
  2. c = c&(0x80000000);
  3. return (!c)*a +(!!c)*b;

} </syntaxhighlight> Ans:


44. What is the following function doing? <syntaxhighlight lang="c"> unsigned fun(unsigned a, unsigned b) {

  1. int i;
  2. unsigned j = 0;
  3. for(i = 0; i < 32; i++)
  4. {
  5. j <<= 1;
  6. j += !!(a & 0x80000000);
  7. a <<= 1;
  8. if(j >=b)
  9. {
  10. j -= b;
  11. a++;
  12. }
  13. }
  14. return a;

} </syntaxhighlight> Ans:


45. What is the following function doing? <syntaxhighlight lang="c"> unsigned fun(unsigned int a) {

  1. unsigned int i, x = 0, y = 0, z = 0;
  2. for(i = 0; i < 16; i++)
  3. {
  4. y <<= 2;
  5. y += !!(a & 0x80000000) << 1;
  6. y += !!(a & 0x40000000);
  7. a <<= 2;
  8. x = x + (x&1);
  9. x <<= 1;
  10. z <<= 1;
  11. if(x + 1 <= y)
  12. {
  13. x++;
  14. z++;

y-=x;

  1. }
  2. }
  3. return z;

} </syntaxhighlight> Ans:



46. Write the code to dynamically allocate a 2-D array of size m x n.

Ans:



47. Declare a pointer to a function accepting an integer and returning void.

Ans:


48. Write the condition so that the below code outputs <math>\unicode{x201C}</math>Hello World<math>\unicode{x201D}</math>. <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. if(<condition>)
  2. {

printf("Hello ");

  1. }
  2. else
  3. {

printf("World\n");

  1. }
  1. return 0;

} </syntaxhighlight> Ans:


49. Write a one line code to check if a number is a power of 2.

Ans:


50. Write a one line code to invert the last four bits of an integer.

Ans:




<quiz display="simple"> {What is the output of this program? <syntaxhighlight lang="c" >

  1. include<stdio.h>

int main() {

  1. char *ptr;
  2. char string[] = "How are you?";
  3. ptr = string;
  4. ptr += 4;
  5. printf("%s",ptr);
  6. return 0;

} </syntaxhighlight> |type="()" /} -(a) How are you?

+(b) are you?

-(c) are

-(d) No output


{ Which of the following will print the value 2 for the above code? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. int a[10][20][30] = {0};
  2. a[5][2][1] = 2;
  1. return 0;

} </syntaxhighlight> |type="()" /} -(a) printf("%d",*(((a+5)+2)+1));

-(b) printf("%d",***((a+5)+2)+1);

+(c) printf("%d",*(*(*(a+5)+2)+1));

-(d) None of these


{ What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. int a = 5;
  2. int b = ++a * a++;
  3. printf("%d ",b);
  4. return 0;

} </syntaxhighlight> |type="()" /} -(a)25

-(b) 30

-(c) 36

+(d) Undefined Behavior


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. int a = 5;
  2. switch(a)
  3. {
  4. default:
  5. a = 4;
  6. case 6:
  7. a--;
  8. case 5:
  9. a = a+1;
  10. case 1:
  11. a = a-1;
  12. }
  13. printf("%d \n",a);
  1. return 0;

} </syntaxhighlight> |type="()" /} +(a) 5

-(b) 4

-(c) 3

-(d) None of these


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. int a = 2,b = 5;
  2. a = a^b;
  3. b = b^a;
  4. printf("%d %d",a,b);
  1. return 0;

} </syntaxhighlight> |type="()" /} -(a) 5 2

-(b) 2 5

-(c) 7 7

+(d) 7 2


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

  1. int a[][3] = {1, 2, 3, 4, 5, 6};
  2. int (*ptr)[3] = a;
  3. printf("%d %d ", (*ptr)[1], (*ptr)[2]);
  4. ++ptr;
  5. printf("%d %d\n", (*ptr)[1], (*ptr)[2]);
  1. return 0;

} </syntaxhighlight> |type="()" /} +(a) 2 3 5 6

-(b) 2 3 4 5

-(c) 4 5 0 0

-(d) none of the above


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

void f(char**);

int main() {

  1. char *argv[] = { "ab", "cd", "ef", "gh", "ij", "kl" };
  2. f(argv);
  1. return 0;

} void f(char **p) {

  1. char *t;
  2. t = (p += sizeof(int))[-1];
  3. printf("%s\n", t);

} </syntaxhighlight> |type="()" /} -(a) ab

-(b) cd

-(c) ef

+(d) gh


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdarg.h>
  2. include <stdio.h>

int ripple(int n, ...) {

  1. int i, j, k;
  2. va_list p;
  3. k = 0;
  4. j = 0;
  5. va_start(p, n);
  6. for (; j < n; ++j)
  7. {
  8. i = va_arg(p, int);
  9. k += i;
  10. }
  11. va_end(p);
  12. return k;

} int main() {

  1. printf("%d\n", ripple(2, 5, 7));
  1. return 0;

} </syntaxhighlight> |type="()" /} +(a) 12

-(b) 5

-(c) 7

-(d) 15


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

int counter(int i) {

  1. static int count = 0;
  2. count = count + i;
  3. return count;

}

int main() {

  1. int i, j;
  2. for (i = 0; i <= 5; i++)
  3. j = counter(i);
  4. printf("%d\n", j);
  1. return 0;

} </syntaxhighlight> |type="()" /} -(a) 10

+(b) 15

-(c) 6

-(d) 7


{ What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. const int x = 5;
  2. const int *ptrx;
  3. ptrx = &x;
  4. *ptrx = 10;
  5. printf("%d\n", x);
  1. return 0;

} </syntaxhighlight> |type="()" /} -(a) 5

-(b) 10

+(c) Compile Error

-(d) Garbage value


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>
  2. define x 4+1

int main() {

  1. int i;
  2. i = x*x*x;
  3. printf("%d",i);
  1. return 0;

} </syntaxhighlight> |type="()" /} -(a) 125

+(b) 13

-(c) 17

-(d) None of above


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. char c=125;
  2. c=c+10;
  3. printf("%d",c);
  1. return 0;

} </syntaxhighlight> |type="()" /} -(a) 135

-(b) +INF

+(c) -121

-(d) -8


{ What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. int i=10;
  2. static int x=i;
  3. if(x==i)
  4. printf("Equal");
  5. else if(x>i)
  6. printf("Greater");
  7. else
  8. printf("Lesser");
  1. return 0;

} </syntaxhighlight> |type="()" /} -(a) Equal

-(b) Greater

-(c) Lesser

+(d) Compile Error


{Consider the following code segment: <syntaxhighlight lang="c">

  1. include <stdlib.h>

int *f1() {

  1. int x = 10;
  2. return &x;

} int *f2() {

  1. int *ptr;
  2. *ptr = 10;
  3. return ptr;

} int *f3() {

  1. int *ptr;
  2. ptr = (int*) malloc(sizeof (*ptr));
  3. return ptr;

} </syntaxhighlight> Which of these functions uses pointers incorrectly? |type="()" /} +(a) f3 only

-(b) f1 and f3

-(c) f1 and f2

-(d) f1, f2, and f3


{ What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

  1. int i = 3;
  2. int j;
  3. j = sizeof(++i + ++i);
  4. printf("i=%d j=%d\n", i, j);
  1. return 0;

} </syntaxhighlight> |type="()" /} -(a) i=4 j=4

+(b) i=3 j=4

-(c) i=5 j=4

-(d) the behavior is undefined


{ What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

void f1(int*, int); void f2(int*, int); void (*p[2])(int*, int);

int main() {

  1. int a = 3;
  2. int b = 5;
  3. p[0] = f1;
  4. p[1] = f2;
  5. p[0](&a, b);
  6. printf("%d %d ", a, b);
  7. p[1](&a, b);
  8. printf("%d %d\n", a, b);
  1. return 0;

}

void f1(int *p, int q) {

  1. int tmp = *p;
  2. *p = q;
  3. q = tmp;

} void f2(int *p, int q) {

  1. int tmp = *p;
  2. *p = q;
  3. q = tmp;

} </syntaxhighlight> |type="()" /} +(a) 5 5 5 5

-(b) 3 5 3 5

-(c) 5 3 3 5

-(d) none of the above


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

void e(int);

int main() {

  1. int a = 3;
  2. e(a);
  3. putchar('\n');
  1. return 0;

} void e(int n) {

  1. if (n > 0)
  2. {
  3. e(--n);
  4. printf("%d ", n);
  5. e(--n);
  6. }

} </syntaxhighlight> |type="()" /} +(a)0 1 2 0

-(b) 0 1 2 1

-(c) 1 2 0 1

-(d) 0 2 1 1


{ Consider the following code segment: <syntaxhighlight lang="c"> typedef int (*test)(float*, float*); test tmp; </syntaxhighlight> What is the type of tmp? |type="()"

/} -(a) function taking two pointer-to-float arguments and returning pointer to int

-(b) pointer to int

+(c) pointer to function taking two pointer-to-float arguments and returning int

-(d) none of the above


{ What is the output of the following program?' <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

  1. char p;
  2. char buf[10] = {1, 2, 3, 4, 5, 6, 9, 8};
  3. p = (buf + 1)[5];
  4. printf("%d\n", p);
  1. return 0;

} </syntaxhighlight> |type="()" /} -(a) 5

-(b) 6

+(c) 9

-(d) none of the above


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

  1. struct node
  2. {
  3. int a;
  4. int b;
  5. int c;
  6. };
  7. struct node s = { 3, 5, 6 };
  8. struct node *pt = &s;
  9. printf("%d\n", *((int*)pt+1));
  1. return 0;

} </syntaxhighlight> |type="()" /} -(a) 3

+(b) 5

-(c) 6

-(d) 7


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

int main(void) {

  1. char a[5] = { 1, 2, 3, 4, 5 };
  2. char *ptr = (char*)(&a + 1);
  3. printf("%d %d\n", *(a + 1), *(ptr - 1));
  1. return 0;

} </syntaxhighlight> |type="()" /} -(a) Compile Error

-(b) 2 1

+(c) 2 5

-(d) none of the above


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

void foo(int[][3]);

int main(void) {

  1. int a[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
  2. foo(a);
  3. printf("%d\n", a[2][1]);
  1. return 0;

}

void foo(int b[][3]) {

  1. ++b;
  2. b[1][1] = 9;

} </syntaxhighlight> |type="()" /} -(a) 8

+(b) 9

-(c) 7

-(d) none of the above


{Consider the following function: <syntaxhighlight lang="c"> int foo(int x, int n) {

  1. int val = 1;
  2. if (n > 0)
  3. {
  4. if (% 2 == 1)
  5. val *= x;
  6. val *= foo(x * x, n / 2);
  7. }
  8. return val;

} </syntaxhighlight>

What function of x and n is computed by foo? |type="()" /} +(a) $x^n$

-(b) $x×n$

-(c) $n^x$

-(d) none of the above

{What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. int a = 0;
  2. switch(a)
  3. {
  4. default:
  5. a = 4;
  6. case 6:
  7. a--;
  8. case 5:
  9. a = a+1;
  10. case 1:
  11. a = a-1;
  12. }
  13. printf("%d \n",a);
  1. return 0;

} </syntaxhighlight> |type="()" /} -(a) 5

-(b) 4

+(c) 3

-(d) 0


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. int a = 2;
  2. if(a == (1,2))
  3. printf("Hello");
  4. if(a == 1,2)
  5. printf("World");
  1. return 0;

} </syntaxhighlight> |type="()" /} -(a) Hello

-(b) World

+(c) HelloWorld

-(d) Compile Error


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. int a = 1,2;
  2. int b = (1,2);
  3. if(a == b)
  4. printf("Equal");
  5. else
  6. printf("Not Equal");
  1. return 0;

} </syntaxhighlight> |type="()" /} -(a) Equal

-(b) Not Equal

-(c) Compiler Dependent

+(d) Compile Error


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

void foo(char *);

int main() {

  1. char *string = "Hello";
  2. foo(string);
  3. printf("%s",string);
  1. return 0;

}

void foo(char *a) {

  1. while(*a)
  2. {
  3. *a += 1;
  4. a++;
  5. }

} </syntaxhighlight> |type="()" /} -(a) Hello

-(b) Ifmmp

-(c) Compile Error

+(d) Segmentation fault


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>
  2. include<stdlib.h>

int main() {

  1. char s[] = "Hello World";
  2. int i = 0;
  3. while(*(s++))
  4. i++;
  5. printf("%d",i);
  1. return 0;

} </syntaxhighlight> |type="()" /} -(a) Segmentation Fault

+(b) Compile Error

-(c) 12

-(d) 0


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int a = 10; int main() {

  1. fun();
  2. fun();
  1. return 0;

}

int fun() {

  1. static int a = 1;
  2. printf("%d ",a);
  3. a++;
  1. return 0;

} </syntaxhighlight> |type="()" /} +(a) 1 2

-(b) 1 1

-(c) 10 11

-(d) 10 10


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>
  2. define crypt(s,t,u,m,p,e,d) m##s##u##t
  3. define begin crypt(a,n,i,m,a,t,e)

int begin() {

  1. printf("Hello\n");
  1. return 0;

} </syntaxhighlight> |type="()" /} +(a) Hello

-(b) Link error

-(c) Segmentation fault

-(d) Compiler error


{Consider the following program: <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. int a[10][20][30]={0};
  2. printf("%ld",&a+1 - &a);
  1. return 0;

} </syntaxhighlight> What is the output of this program? |type="{}" /} { 1 }


{Consider the following program: <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. int a[10][20][30] = {0};
  2. int *b = a;
  3. int *c = a+1;
  4. printf("%ld", c-b);
  1. return 0;

} </syntaxhighlight> What is the output of this program? (You may ignore compiler warnings) |type="{}" /} { 600 }


{ What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>
  2. include<stdlib.h>

int* fun();

int main() {

  1. int *a = fun();
  2. printf("%d",*a);
  1. return 0;

} int* fun() {

  1. int *a =(int*) malloc(sizeof(int));
  2. *a = 10;
  3. return a;

} </syntaxhighlight> |type="{}" /} { 10 }


{ What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. int *a = fun();
  2. printf("%d",a);
  1. return 0;

} int fun() {

  1. int a = 10;
  2. return a;

} </syntaxhighlight> |type="{}" /} { 10 }


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>
  2. include<string.h>

int main() {

  1. char string[] = "Hello";
  2. printf("%zu %zu",sizeof(string),strlen(string));
  1. return 0;

} </syntaxhighlight> |type="{}" /} { 6 5 }


{ What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. float a = 0.5;
  2. if(a == 0.5)
  3. printf("Yes");
  4. else
  5. printf("No");
  1. return 0;

} </syntaxhighlight> |type="{}" /} { Yes }


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>
  2. include<string.h>

void foo(char *);

int main() {

  1. char a[100] = {0};
  2. printf("%lu %lu",sizeof(a),strlen(a));
  1. return 0;

} </syntaxhighlight> |type="{}" /} { 1 }



{What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. int a;
  2. printf("%d",scanf("%d",&a));
  1. return 0;

} </syntaxhighlight> |type="{}" /} { 1 }


{If the binary equivalent of 5.375 in normalised form is 0100 0000 1010 1100 0000 0000 0000 0000, what will be the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>
  2. include<math.h>

int main() {

  1. float a=5.375;
  2. char *p;
  3. int i;
  4. p = (char*)&a;
  5. for(i=0; i<2; i++)
  6. printf("%x ", (unsigned char)(p[i]^p[3-i]));
  1. return 0;

} </syntaxhighlight> |type="{}" /} { 40ac }


{ What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. char str[] = {'a','b','c','\0'};
  2. str[0] -= 32;
  3. printf("%s",str);
  1. return 0;

} </syntaxhighlight> |type="{}" /} { Abc }

</quiz>

Some codes[edit]

1. What is the following function doing? <syntaxhighlight lang="c"> int foo(int n) {

  1. int sum = 0;
  2. while(n > 0)
  3. {
  4. n = n & n-1;
  5. sum++;
  6. }
  7. return sum;

} </syntaxhighlight> Ans:


2. What is the following function doing? <syntaxhighlight lang="c"> int foo(int a, int b) {

  1. int c = a, d = b;
  2. while(!= b)
  3. {
  4. if(a < b)
  5. a = a+c;
  6. else
  7. b = b+d;
  8. }
  9. return a;

} </syntaxhighlight> Ans:


43. What is the following function doing? <syntaxhighlight lang="c"> int foo( int a, int b) {

  1. int c = a-b;
  2. c = c&(0x80000000);
  3. return (!c)*a +(!!c)*b;

} </syntaxhighlight> Ans:


44. What is the following function doing? <syntaxhighlight lang="c"> unsigned fun(unsigned a, unsigned b) {

  1. int i;
  2. unsigned j = 0;
  3. for(i = 0; i < 32; i++)
  4. {
  5. j <<= 1;
  6. j += !!(a & 0x80000000);
  7. a <<= 1;
  8. if(j >=b)
  9. {
  10. j -= b;
  11. a++;
  12. }
  13. }
  14. return a;

} </syntaxhighlight> Ans:


45. What is the following function doing? <syntaxhighlight lang="c"> unsigned fun(unsigned int a) {

  1. unsigned int i, x = 0, y = 0, z = 0;
  2. for(i = 0; i < 16; i++)
  3. {
  4. y <<= 2;
  5. y += !!(a & 0x80000000) << 1;
  6. y += !!(a & 0x40000000);
  7. a <<= 2;
  8. x = x + (x&1);
  9. x <<= 1;
  10. z <<= 1;
  11. if(x + 1 <= y)
  12. {
  13. x++;
  14. z++;

y-=x;

  1. }
  2. }
  3. return z;

} </syntaxhighlight> Ans:



46. Write the code to dynamically allocate a 2-D array of size m x n.

Ans:



47. Declare a pointer to a function accepting an integer and returning void.

Ans:


48. Write the condition so that the below code outputs <math>\unicode{x201C}</math>Hello World<math>\unicode{x201D}</math>. <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  1. if(<condition>)
  2. {

printf("Hello ");

  1. }
  2. else
  3. {

printf("World\n");

  1. }
  1. return 0;

} </syntaxhighlight> Ans:


49. Write a one line code to check if a number is a power of 2.

Ans:


50. Write a one line code to invert the last four bits of an integer.

Ans:




blog comments powered by Disqus