Line 390: | Line 390: | ||
int main() | int main() | ||
{ | { | ||
− | int a = 3; | + | int a = 3; |
− | int b = 5; | + | int b = 5; |
− | p[0] = f1; | + | p[0] = f1; |
− | p[1] = f2; | + | p[1] = f2; |
− | p[0](&a, b); | + | p[0](&a, b); |
− | printf("%d %d ", a, b); | + | printf("%d %d ", a, b); |
− | p[1](&a, b); | + | p[1](&a, b); |
− | printf("%d %d\n", a, b); | + | printf("%d %d\n", a, b); |
− | return 0; | + | |
+ | return 0; | ||
} | } | ||
void f1(int *p, int q) | void f1(int *p, int q) | ||
{ | { | ||
− | int tmp = *p; | + | int tmp = *p; |
− | *p = q; | + | *p = q; |
− | q = tmp; | + | q = tmp; |
} | } | ||
void f2(int *p, int q) | void f2(int *p, int q) | ||
{ | { | ||
− | int tmp = *p; | + | int tmp = *p; |
− | *p = q; | + | *p = q; |
− | q = tmp; | + | q = tmp; |
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
(a) 5 5 5 5 | (a) 5 5 5 5 | ||
+ | |||
(b) 3 5 3 5 | (b) 3 5 3 5 | ||
+ | |||
(c) 5 3 3 5 | (c) 5 3 3 5 | ||
+ | |||
(d) none of the above | (d) none of the above | ||
+ | |||
17. What is the output of the following program? | 17. What is the output of the following program? | ||
Line 426: | Line 431: | ||
int main() | int main() | ||
{ | { | ||
− | int a = 3; | + | int a = 3; |
− | e(a); | + | e(a); |
− | putchar('\n'); | + | putchar('\n'); |
− | return 0; | + | |
+ | return 0; | ||
} | } | ||
void e(int n) | void e(int n) | ||
{ | { | ||
− | if (n > 0) | + | if (n > 0) |
− | { | + | { |
− | e(--n); | + | e(--n); |
− | printf("%d ", n); | + | printf("%d ", n); |
− | e(--n); | + | e(--n); |
− | } | + | } |
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
(a) 0 1 2 0 | (a) 0 1 2 0 | ||
+ | |||
(b) 0 1 2 1 | (b) 0 1 2 1 | ||
+ | |||
(c) 1 2 0 1 | (c) 1 2 0 1 | ||
+ | |||
(d) 0 2 1 1 | (d) 0 2 1 1 | ||
+ | |||
18. Consider the following code segment: | 18. Consider the following code segment: | ||
Line 452: | Line 462: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
What is the type of tmp? | What is the type of tmp? | ||
+ | |||
(a) function taking two pointer-to-float arguments and returning pointer to int | (a) function taking two pointer-to-float arguments and returning pointer to int | ||
+ | |||
(b) pointer to int | (b) pointer to int | ||
+ | |||
(c) pointer to function taking two pointer-to-float arguments and returning int | (c) pointer to function taking two pointer-to-float arguments and returning int | ||
+ | |||
(d) none of the above | (d) none of the above | ||
+ | |||
19. What is the output of the following program? | 19. What is the output of the following program? | ||
Line 464: | Line 479: | ||
int main() | int main() | ||
{ | { | ||
− | char p; | + | char p; |
− | char buf[10] = {1, 2, 3, 4, 5, 6, 9, 8}; | + | char buf[10] = {1, 2, 3, 4, 5, 6, 9, 8}; |
− | p = (buf + 1)[5]; | + | p = (buf + 1)[5]; |
− | printf("%d\n", p); | + | printf("%d\n", p); |
− | return 0; | + | |
+ | return 0; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
(a) 5 | (a) 5 | ||
+ | |||
(b) 6 | (b) 6 | ||
+ | |||
(c) 9 | (c) 9 | ||
+ | |||
(d) none of the above | (d) none of the above | ||
+ | |||
20. What is the output of the following program? | 20. What is the output of the following program? | ||
Line 481: | Line 501: | ||
int main() | int main() | ||
{ | { | ||
− | struct node | + | struct node |
− | { | + | { |
− | int a; | + | int a; |
− | int b; | + | int b; |
− | int c; | + | int c; |
− | }; | + | }; |
− | struct node s = { 3, 5, 6 }; | + | struct node s = { 3, 5, 6 }; |
− | struct node *pt = &s; | + | struct node *pt = &s; |
− | printf("%d\n", *((int*)pt+1)); | + | printf("%d\n", *((int*)pt+1)); |
− | return 0; | + | |
+ | return 0; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
(a) 3 | (a) 3 | ||
+ | |||
(b) 5 | (b) 5 | ||
+ | |||
(c) 6 | (c) 6 | ||
+ | |||
(d) 7 | (d) 7 | ||
+ | |||
21. What is the output of the following program? | 21. What is the output of the following program? | ||
Line 503: | Line 528: | ||
int main(void) | int main(void) | ||
{ | { | ||
− | char a[5] = { 1, 2, 3, 4, 5 }; | + | char a[5] = { 1, 2, 3, 4, 5 }; |
− | char *ptr = (char*)(&a + 1); | + | char *ptr = (char*)(&a + 1); |
− | printf("%d %d\n", *(a + 1), *(ptr - 1)); | + | printf("%d %d\n", *(a + 1), *(ptr - 1)); |
− | return 0; | + | |
+ | return 0; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
(a) Compile Error | (a) Compile Error | ||
+ | |||
(b) 2 1 | (b) 2 1 | ||
+ | |||
(c) 2 5 | (c) 2 5 | ||
+ | |||
(d) none of the above | (d) none of the above | ||
+ | |||
22. What is the output of the following program? | 22. What is the output of the following program? | ||
Line 521: | Line 551: | ||
int main(void) | int main(void) | ||
{ | { | ||
− | int a[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; | + | int a[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; |
− | foo(a); | + | foo(a); |
− | printf("%d\n", a[2][1]); | + | printf("%d\n", a[2][1]); |
− | return 0; | + | |
+ | return 0; | ||
} | } | ||
void foo(int b[][3]) | void foo(int b[][3]) | ||
{ | { | ||
− | ++b; | + | ++b; |
− | b[1][1] = 9; | + | b[1][1] = 9; |
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
(a) 8 | (a) 8 | ||
+ | |||
(b) 9 | (b) 9 | ||
+ | |||
(c) 7 | (c) 7 | ||
+ | |||
(d) none of the above | (d) none of the above | ||
+ | |||
23. Consider the following function: | 23. Consider the following function: | ||
Line 542: | Line 577: | ||
int foo(int x, int n) | int foo(int x, int n) | ||
{ | { | ||
− | int val = 1; | + | int val = 1; |
− | if (n > 0) | + | if (n > 0) |
− | { | + | { |
− | if (n % 2 == 1) | + | if (n % 2 == 1) |
− | val *= x; | + | val *= x; |
− | val *= foo(x * x, n / 2); | + | val *= foo(x * x, n / 2); |
− | } | + | } |
− | return val; | + | return val; |
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
What function of x and n is computed by foo? | What function of x and n is computed by foo? | ||
(a) x^n | (a) x^n | ||
+ | |||
(b) x×n | (b) x×n | ||
+ | |||
(c) nx | (c) nx | ||
+ | |||
(d) none of the above | (d) none of the above | ||
Line 564: | Line 602: | ||
int main() | int main() | ||
{ | { | ||
− | int a = 0; | + | int a = 0; |
− | switch(a) | + | switch(a) |
− | { | + | { |
− | default: | + | default: |
− | a = 4; | + | a = 4; |
− | case 6: | + | case 6: |
− | a--; | + | a--; |
− | case 5: | + | case 5: |
− | a = a+1; | + | a = a+1; |
− | case 1: | + | case 1: |
− | a = a-1; | + | a = a-1; |
− | } | + | } |
− | + | printf("%d \n",a); | |
− | return 0; | + | |
+ | return 0; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
(a) 5 | (a) 5 | ||
+ | |||
(b) 4 | (b) 4 | ||
+ | |||
(c) 3 | (c) 3 | ||
+ | |||
(d) 0 | (d) 0 | ||
+ | |||
25. What is the output of the following program? | 25. What is the output of the following program? | ||
Line 590: | Line 633: | ||
int main() | int main() | ||
{ | { | ||
− | int a = 2; | + | int a = 2; |
− | if(a == (1,2)) | + | if(a == (1,2)) |
− | printf("Hello"); | + | printf("Hello"); |
− | if(a == 1,2) | + | if(a == 1,2) |
− | printf("World"); | + | printf("World"); |
− | return 0; | + | |
+ | return 0; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
(a) Hello | (a) Hello | ||
+ | |||
(b) World | (b) World | ||
+ | |||
(c) Hello World | (c) Hello World | ||
+ | |||
(d) Compile Error | (d) Compile Error | ||
+ | |||
26. What is the output of the following program? | 26. What is the output of the following program? | ||
Line 608: | Line 656: | ||
int main() | int main() | ||
{ | { | ||
− | int a = 1,2; | + | int a = 1,2; |
− | int b = (1,2); | + | int b = (1,2); |
− | if(a == b) | + | if(a == b) |
− | printf("Equal"); | + | printf("Equal"); |
− | else | + | else |
− | printf("Not Equal"); | + | printf("Not Equal"); |
− | return 0; | + | |
+ | return 0; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
(a) Equal | (a) Equal | ||
+ | |||
(b) Not Equal | (b) Not Equal | ||
+ | |||
(c) Compiler Dependent | (c) Compiler Dependent | ||
+ | |||
(d) Compile Error | (d) Compile Error | ||
+ | |||
27. What is the output of the following program? | 27. What is the output of the following program? | ||
Line 629: | Line 682: | ||
int main() | int main() | ||
{ | { | ||
− | char *string = "Hello"; | + | char *string = "Hello"; |
− | foo(string); | + | foo(string); |
− | printf("%s",string); | + | printf("%s",string); |
− | return 0; | + | |
+ | return 0; | ||
} | } | ||
void foo(char *a) | void foo(char *a) | ||
{ | { | ||
− | while(*a) | + | while(*a) |
− | { | + | { |
− | *a += 1; | + | *a += 1; |
− | a++; | + | a++; |
− | } | + | } |
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
(a) Hello | (a) Hello | ||
+ | |||
(b) Ifmmp | (b) Ifmmp | ||
+ | |||
(c) Compile Error | (c) Compile Error | ||
+ | |||
(d) Segmentation fault | (d) Segmentation fault | ||
+ | |||
28. What is the output of the following program? | 28. What is the output of the following program? | ||
Line 655: | Line 713: | ||
int main() | int main() | ||
{ | { | ||
− | char s[] = "Opendays2012"; | + | char s[] = "Opendays2012"; |
− | int i = 0; | + | int i = 0; |
− | while(*(s++)) | + | while(*(s++)) |
− | i++; | + | i++; |
− | printf("%d",i); | + | printf("%d",i); |
− | return 0; | + | |
+ | return 0; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
(a) Segmentation Fault | (a) Segmentation Fault | ||
+ | |||
(b) Compile Error | (b) Compile Error | ||
+ | |||
(c) 12 | (c) 12 | ||
+ | |||
(d) 0 | (d) 0 | ||
+ | |||
29. What is the output of the following program? | 29. What is the output of the following program? | ||
Line 674: | Line 737: | ||
int main() | int main() | ||
{ | { | ||
− | fun(); | + | fun(); |
− | fun(); | + | fun(); |
− | return 0; | + | |
+ | return 0; | ||
} | } | ||
int fun() | int fun() | ||
{ | { | ||
− | static int a = 1; | + | static int a = 1; |
− | printf("%d ",a); | + | printf("%d ",a); |
− | a++; | + | a++; |
− | return 0; | + | |
+ | return 0; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
(a) 1 2 | (a) 1 2 | ||
+ | |||
(b) 1 1 | (b) 1 1 | ||
+ | |||
(c) 10 11 | (c) 10 11 | ||
+ | |||
(d) 10 10 | (d) 10 10 | ||
+ | |||
30. What is the output of the following program? | 30. What is the output of the following program? | ||
Line 699: | Line 768: | ||
int begin() | int begin() | ||
{ | { | ||
− | printf("Hello\n"); | + | printf("Hello\n"); |
− | return 0; | + | |
+ | return 0; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
(a) Hello | (a) Hello | ||
+ | |||
(b) Link error | (b) Link error | ||
+ | |||
(c) Segmentation fault | (c) Segmentation fault | ||
+ | |||
(d) Compiler error | (d) Compiler error | ||
+ | |||
31. Consider the following program: | 31. Consider the following program: | ||
Line 713: | Line 787: | ||
int main() | int main() | ||
{ | { | ||
− | int a[10][20][30]={0}; | + | int a[10][20][30]={0}; |
− | printf("%ld",&a+1 - &a); | + | printf("%ld",&a+1 - &a); |
− | return 0; | + | |
+ | return 0; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 727: | Line 802: | ||
int main() | int main() | ||
{ | { | ||
− | int a[10][20][30] = {0}; | + | int a[10][20][30] = {0}; |
− | int *b = a; | + | int *b = a; |
− | int *c = a+1; | + | int *c = a+1; |
− | printf("%ld", c-b); | + | printf("%ld", c-b); |
− | return 0; | + | |
+ | return 0; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 748: | Line 824: | ||
int main() | int main() | ||
{ | { | ||
− | int *a = fun(); | + | int *a = fun(); |
− | printf("%d",*a); | + | printf("%d",*a); |
− | return 0; | + | |
+ | return 0; | ||
} | } | ||
int* fun() | int* fun() | ||
{ | { | ||
− | int *a =(int*) malloc(sizeof(int)); | + | int *a =(int*) malloc(sizeof(int)); |
− | *a = 10; | + | *a = 10; |
− | return a; | + | return a; |
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 768: | Line 845: | ||
int main() | int main() | ||
{ | { | ||
− | int *a = fun(); | + | int *a = fun(); |
− | printf("%d",*a); | + | printf("%d",*a); |
− | return 0; | + | |
+ | return 0; | ||
} | } | ||
int fun() | int fun() | ||
{ | { | ||
− | int a = 10; | + | int a = 10; |
− | return a; | + | return a; |
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 789: | Line 867: | ||
int main() | int main() | ||
{ | { | ||
− | char string[] = "Hello"; | + | char string[] = "Hello"; |
− | printf("%lu %lu",sizeof(string),strlen(string)); | + | printf("%lu %lu",sizeof(string),strlen(string)); |
− | return 0; | + | |
+ | return 0; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 803: | Line 882: | ||
int main() | int main() | ||
{ | { | ||
− | float a = 0.5; | + | float a = 0.5; |
− | if(a == 0.5) | + | if(a == 0.5) |
− | printf("Yes"); | + | printf("Yes"); |
− | else | + | else |
− | printf("No"); | + | printf("No"); |
− | return 0; | + | |
+ | return 0; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 823: | Line 903: | ||
int main() | int main() | ||
{ | { | ||
− | char a[100] = {0}; | + | char a[100] = {0}; |
− | printf("%lu %lu",sizeof(a),strlen(a)); | + | printf("%lu %lu",sizeof(a),strlen(a)); |
− | return 0; | + | |
+ | return 0; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 839: | Line 920: | ||
int main() | int main() | ||
{ | { | ||
− | + | int a; | |
− | printf("%d",scanf("%d",&a)); | + | printf("%d",scanf("%d",&a)); |
− | return 0; | + | |
+ | return 0; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 856: | Line 938: | ||
int main() | int main() | ||
{ | { | ||
− | float a=5.375; | + | float a=5.375; |
− | char *p; | + | char *p; |
− | int i; | + | int i; |
− | p = (char*)&a; | + | p = (char*)&a; |
− | for(i=0; i<2; i++) | + | for(i=0; i<2; i++) |
− | printf("%02x ", (unsigned char)(p[i]^p[3-i])); | + | printf("%02x ", (unsigned char)(p[i]^p[3-i])); |
− | return 0; | + | |
+ | return 0; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 873: | Line 956: | ||
int main() | int main() | ||
{ | { | ||
− | + | char str[] = {'a','b','c','\0'}; | |
− | str[0] -= 32; | + | str[0] -= 32; |
− | printf("%s",str); | + | printf("%s",str); |
− | return 0; | + | |
+ | return 0; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 888: | Line 972: | ||
int foo(int n) | int foo(int n) | ||
{ | { | ||
− | int sum = 0; | + | int sum = 0; |
− | while(n > 0) | + | while(n > 0) |
− | { | + | { |
− | n = n & n-1; | + | n = n & n-1; |
− | sum++; | + | sum++; |
− | } | + | } |
− | return sum; | + | return sum; |
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 905: | Line 989: | ||
int foo(int a, int b) | int foo(int a, int b) | ||
{ | { | ||
− | int c = a, d = b; | + | int c = a, d = b; |
− | while(a != b) | + | while(a != b) |
− | { | + | { |
− | if(a < b) | + | if(a < b) |
− | a = a+c; | + | a = a+c; |
− | else | + | else |
− | b = b+d; | + | b = b+d; |
− | } | + | } |
− | return a; | + | return a; |
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 924: | Line 1,008: | ||
int foo( int a, int b) | int foo( int a, int b) | ||
{ | { | ||
− | int c = a-b; | + | int c = a-b; |
− | c = c&(0x80000000); | + | c = c&(0x80000000); |
− | return (!c)*a +(!!c)*b; | + | return (!c)*a +(!!c)*b; |
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 937: | Line 1,021: | ||
unsigned fun(unsigned a, unsigned b) | unsigned fun(unsigned a, unsigned b) | ||
{ | { | ||
− | + | int i; | |
− | + | unsigned j = 0; | |
− | + | for(i = 0; i < 32; i++) | |
− | + | { | |
− | + | j <<= 1; | |
− | + | j += !!(a & 0x80000000); | |
− | + | a <<= 1; | |
− | + | if(j >=b) | |
− | + | { | |
− | + | j -= b; | |
− | + | a++; | |
− | + | } | |
− | + | } | |
− | + | return a; | |
− | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 962: | Line 1,045: | ||
unsigned fun(unsigned int a) | unsigned fun(unsigned int a) | ||
{ | { | ||
− | + | unsigned int i, x = 0, y = 0, z = 0; | |
− | + | for(i = 0; i < 16; i++) | |
− | + | { | |
− | + | y <<= 2; | |
− | + | y += !!(a & 0x80000000) << 1; | |
− | + | y += !!(a & 0x40000000); | |
− | + | a <<= 2; | |
− | + | x = x + (x&1); | |
− | + | x <<= 1; | |
− | + | z <<= 1; | |
− | + | if(x + 1 <= y) | |
− | + | { | |
− | + | x++; | |
− | + | z++; | |
− | + | y-=x; | |
− | + | } | |
− | + | } | |
− | + | return z; | |
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 1,005: | Line 1,088: | ||
int main() | int main() | ||
{ | { | ||
− | if(<condition>) | + | if(<condition>) |
− | { | + | { |
printf("Hello "); | printf("Hello "); | ||
− | } | + | } |
− | else | + | else |
− | { | + | { |
printf("World\n"); | printf("World\n"); | ||
− | } | + | } |
− | return 0; | + | |
+ | return 0; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 1,028: | Line 1,112: | ||
Ans: | Ans: | ||
+ | |||
+ | |||
+ | <disqus></disqus> | ||
+ | |||
+ | [[Category:GATE]] | ||
+ | [[Category:Code]] |
1. What is the output of this program? <syntaxhighlight lang="c">
int main() {
char *ptr; char string[] = "How are you?"; ptr = string; ptr += 4; printf("%s",ptr); return 0;
} </syntaxhighlight> (a) How are you?
(b) are you?
(c) are
(d) No output
2. Which of the following will print the value 2 for the above code?
<syntaxhighlight lang="c">
int main() {
int a[10][20][30] = {0}; a[5][2][1] = 2;
return 0;
} </syntaxhighlight> (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
3. What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int a = 5; int b = ++a * a++; printf("%d ",b); return 0;
} </syntaxhighlight> (a) 25
(b) 30
(c) 36
(d) Undefined Behavior
4. What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int a = 5; switch(a) { default: a = 4; case 6: a--; case 5: a = a+1; case 1: a = a-1; } printf("%d \n",a);
return 0;
} </syntaxhighlight> (a) 5
(b) 4
(c) 3
(d) None of these
5. What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int a = 2,b = 5; a = a^b; b = b^a; printf("%d %d",a,b);
return 0;
} </syntaxhighlight> (a) 5 2
(b) 2 5
(c) 7 7
(d) 7 2
6. What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int a[][3] = {1, 2, 3, 4, 5, 6}; int (*ptr)[3] = a; printf("%d %d ", (*ptr)[1], (*ptr)[2]); ++ptr; printf("%d %d\n", (*ptr)[1], (*ptr)[2]);
return 0;
} </syntaxhighlight> (a) 2 3 5 6
(b) 2 3 4 5
(c) 4 5 0 0
(d) none of the above
7. What is the output of the following program?
<syntaxhighlight lang="c">
void f(char**);
int main() {
char *argv[] = { "ab", "cd", "ef", "gh", "ij", "kl" }; f(argv);
return 0;
} void f(char **p) {
char *t; t = (p += sizeof(int))[-1]; printf("%s\n", t);
} </syntaxhighlight> (a) ab
(b) cd
(c) ef
(d) gh
8. What is the output of the following program?
<syntaxhighlight lang="c">
int ripple(int n, ...) {
int i, j, k; va_list p; k = 0; j = 1; va_start(p, n); for (; j < n; ++j) { i = va_arg(p, int); k += i; } va_end(p); return k;
} int main() {
printf("%d\n", ripple(3, 5, 7));
return 0;
} </syntaxhighlight> (a) 12
(b) 5
(c) 7
(d) 15
9. What is the output of the following program?
<syntaxhighlight lang="c">
int counter(int i) {
static int count = 0; count = count + i; return count;
}
int main() {
int i, j; for (i = 0; i <= 5; i++) j = counter(i); printf("%d\n", j);
return 0;
} </syntaxhighlight> (a) 10
(b) 15
(c) 6
(d) 7
10. What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
const int x = 5; const int *ptrx; ptrx = &x; *ptrx = 10; printf("%d\n", x);
return 0;
} </syntaxhighlight> (a) 5
(b) 10
(c) Compile Error
(d) Garbage value
11. What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int i; i = x*x*x; printf("%d",i);
return 0;
} </syntaxhighlight> (a) 125
(b) 13
(c) 17
(d) None of above
12. What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
char c=125; c=c+10; printf("%d",c);
return 0;
} </syntaxhighlight> (a) 135
(b) +INF
(c) -121
(d) -8
13. What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int i=10; static int x=i; if(x==i) printf("Equal"); else if(x>i) printf("Greater"); else printf("Lesser");
return 0;
} </syntaxhighlight> (a) Equal
(b) Greater
(c) Lesser
(d) Compile Error
14. Consider the following code segment:
<syntaxhighlight lang="c">
int *f1() {
int x = 10; return &x;
} int *f2() {
int *ptr; *ptr = 10; return ptr;
} int *f3() {
int *ptr; ptr = (int*) malloc(sizeof (*ptr)); return ptr;
} </syntaxhighlight> Which of these functions uses pointers incorrectly?
(a) f3 only
(b) f1 and f3
(c) f1 and f2
(d) f1, f2, and f3
15. What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int i = 3; int j; j = sizeof(++i + ++i); printf("i=%d j=%d\n", i, j);
return 0;
} </syntaxhighlight> (a) i=4 j=4
(b) i=3 j=4
(c) i=5 j=4
(d) the behavior is undefined
16. What is the output of the following program?
<syntaxhighlight lang="c">
void f1(int*, int); void f2(int*, int); void (*p[2])(int*, int);
int main() {
int a = 3; int b = 5; p[0] = f1; p[1] = f2; p[0](&a, b); printf("%d %d ", a, b); p[1](&a, b); printf("%d %d\n", a, b);
return 0;
}
void f1(int *p, int q) {
int tmp = *p; *p = q; q = tmp;
} void f2(int *p, int q) {
int tmp = *p; *p = q; q = tmp;
} </syntaxhighlight> (a) 5 5 5 5
(b) 3 5 3 5
(c) 5 3 3 5
(d) none of the above
17. What is the output of the following program?
<syntaxhighlight lang="c">
void e(int);
int main() {
int a = 3; e(a); putchar('\n');
return 0;
} void e(int n) {
if (n > 0) { e(--n); printf("%d ", n); e(--n); }
} </syntaxhighlight> (a) 0 1 2 0
(b) 0 1 2 1
(c) 1 2 0 1
(d) 0 2 1 1
18. Consider the following code segment:
<syntaxhighlight lang="c">
typedef int (*test)(float*, float*);
test tmp;
</syntaxhighlight>
What is the type of tmp?
(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
19. What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
char p; char buf[10] = {1, 2, 3, 4, 5, 6, 9, 8}; p = (buf + 1)[5]; printf("%d\n", p);
return 0;
} </syntaxhighlight> (a) 5
(b) 6
(c) 9
(d) none of the above
20. What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
struct node { int a; int b; int c; }; struct node s = { 3, 5, 6 }; struct node *pt = &s; printf("%d\n", *((int*)pt+1));
return 0;
} </syntaxhighlight> (a) 3
(b) 5
(c) 6
(d) 7
21. What is the output of the following program?
<syntaxhighlight lang="c">
int main(void) {
char a[5] = { 1, 2, 3, 4, 5 }; char *ptr = (char*)(&a + 1); printf("%d %d\n", *(a + 1), *(ptr - 1));
return 0;
} </syntaxhighlight> (a) Compile Error
(b) 2 1
(c) 2 5
(d) none of the above
22. What is the output of the following program?
<syntaxhighlight lang="c">
void foo(int[][3]);
int main(void) {
int a[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; foo(a); printf("%d\n", a[2][1]);
return 0;
}
void foo(int b[][3]) {
++b; b[1][1] = 9;
} </syntaxhighlight> (a) 8
(b) 9
(c) 7
(d) none of the above
23. Consider the following function:
<syntaxhighlight lang="c">
int foo(int x, int n)
{
int val = 1; if (n > 0) { if (n % 2 == 1) val *= x; val *= foo(x * x, n / 2); } return val;
} </syntaxhighlight> What function of x and n is computed by foo? (a) x^n
(b) x×n
(c) nx
(d) none of the above
24. What is the output of the following program? <syntaxhighlight lang="c">
int main() {
int a = 0; switch(a) { default: a = 4; case 6: a--; case 5: a = a+1; case 1: a = a-1; } printf("%d \n",a);
return 0;
} </syntaxhighlight> (a) 5
(b) 4
(c) 3
(d) 0
25. What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int a = 2; if(a == (1,2)) printf("Hello"); if(a == 1,2) printf("World");
return 0;
} </syntaxhighlight> (a) Hello
(b) World
(c) Hello World
(d) Compile Error
26. What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int a = 1,2; int b = (1,2); if(a == b) printf("Equal"); else printf("Not Equal");
return 0;
} </syntaxhighlight> (a) Equal
(b) Not Equal
(c) Compiler Dependent
(d) Compile Error
27. What is the output of the following program?
<syntaxhighlight lang="c">
void foo(char *);
int main() {
char *string = "Hello"; foo(string); printf("%s",string);
return 0;
}
void foo(char *a) {
while(*a) { *a += 1; a++; }
} </syntaxhighlight> (a) Hello
(b) Ifmmp
(c) Compile Error
(d) Segmentation fault
28. What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
char s[] = "Opendays2012"; int i = 0; while(*(s++)) i++; printf("%d",i);
return 0;
} </syntaxhighlight> (a) Segmentation Fault
(b) Compile Error
(c) 12
(d) 0
29. What is the output of the following program?
<syntaxhighlight lang="c">
int a = 10; int main() {
fun(); fun();
return 0;
}
int fun() {
static int a = 1; printf("%d ",a); a++;
return 0;
} </syntaxhighlight> (a) 1 2
(b) 1 1
(c) 10 11
(d) 10 10
30. What is the output of the following program?
<syntaxhighlight lang="c">
int begin() {
printf("Hello\n");
return 0;
} </syntaxhighlight> (a) Hello
(b) Link error
(c) Segmentation fault
(d) Compiler error
31. Consider the following program:
<syntaxhighlight lang="c">
int main() {
int a[10][20][30]={0}; printf("%ld",&a+1 - &a);
return 0;
} </syntaxhighlight> What is the output of this program? Ans:
32. Consider the following program:
<syntaxhighlight lang="c">
int main() {
int a[10][20][30] = {0}; int *b = a; int *c = a+1; printf("%ld", c-b);
return 0;
} </syntaxhighlight> What is the output of this program? (You may ignore compiler warnings)
Ans:
33. Consider the following program:
<syntaxhighlight lang="c">
int* fun();
int main() {
int *a = fun(); printf("%d",*a);
return 0;
} int* fun() {
int *a =(int*) malloc(sizeof(int)); *a = 10; return a;
} </syntaxhighlight> What is the output of this program? Ans:
34. Consider the following program:
<syntaxhighlight lang="c">
int main() {
int *a = fun(); printf("%d",*a);
return 0;
} int fun() {
int a = 10; return a;
} </syntaxhighlight> What is the output of this program? Ans:
35. Consider the following program:
<syntaxhighlight lang="c">
int main() {
char string[] = "Hello"; printf("%lu %lu",sizeof(string),strlen(string));
return 0;
} </syntaxhighlight> What is the output of this program? Ans:
36. Consider the following program:
<syntaxhighlight lang="c">
int main() {
float a = 0.5; if(a == 0.5) printf("Yes"); else printf("No");
return 0;
} </syntaxhighlight> What is the output of this program? Ans:
37. Consider the following program:
<syntaxhighlight lang="c">
void foo(char *);
int main() {
char a[100] = {0}; printf("%lu %lu",sizeof(a),strlen(a));
return 0;
} </syntaxhighlight> What is the output of this program? Ans:
38. Consider the following program:
<syntaxhighlight lang="c">
int main() {
int a; printf("%d",scanf("%d",&a));
return 0;
} </syntaxhighlight> What is the output of the above code?
Ans:
39. 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 program ? <syntaxhighlight lang="c">
int main() {
float a=5.375; char *p; int i; p = (char*)&a; for(i=0; i<2; i++) printf("%02x ", (unsigned char)(p[i]^p[3-i]));
return 0;
} </syntaxhighlight> Ans:
40. Consider the following program:
<syntaxhighlight lang="c">
int main() {
char str[] = {'a','b','c','\0'}; str[0] -= 32; printf("%s",str);
return 0;
} </syntaxhighlight> What is the output of the above code?
Ans:
41. What is the following function doing?
<syntaxhighlight lang="c">
int foo(int n)
{
int sum = 0; while(n > 0) { n = n & n-1; sum++; } return sum;
} </syntaxhighlight> Ans:
42. What is the following function doing? <syntaxhighlight lang="c"> int foo(int a, int b) {
int c = a, d = b; while(a != b) { if(a < b) a = a+c; else b = b+d; } return a;
} </syntaxhighlight> Ans:
43. What is the following function doing? <syntaxhighlight lang="c"> int foo( int a, int b) {
int c = a-b; c = c&(0x80000000); return (!c)*a +(!!c)*b;
} </syntaxhighlight> Ans:
44. What is the following function doing? <syntaxhighlight lang="c"> unsigned fun(unsigned a, unsigned b) {
int i; unsigned j = 0; for(i = 0; i < 32; i++) { j <<= 1; j += !!(a & 0x80000000); a <<= 1; if(j >=b) { j -= b; a++; } } return a;
} </syntaxhighlight> Ans:
45. What is the following function doing? <syntaxhighlight lang="c"> unsigned fun(unsigned int a) {
unsigned int i, x = 0, y = 0, z = 0; for(i = 0; i < 16; i++) { y <<= 2; y += !!(a & 0x80000000) << 1; y += !!(a & 0x40000000); a <<= 2; x = x + (x&1); x <<= 1; z <<= 1; if(x + 1 <= y) { x++; z++;
y-=x;
} } 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 “Hello World”. <syntaxhighlight lang="c">
int main() {
if(<condition>) {
printf("Hello ");
} else {
printf("World\n");
}
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:
1. What is the output of this program? <syntaxhighlight lang="c">
int main() {
char *ptr; char string[] = "How are you?"; ptr = string; ptr += 4; printf("%s",ptr); return 0;
} </syntaxhighlight> (a) How are you?
(b) are you?
(c) are
(d) No output
2. Which of the following will print the value 2 for the above code?
<syntaxhighlight lang="c">
int main() {
int a[10][20][30] = {0}; a[5][2][1] = 2;
return 0;
} </syntaxhighlight> (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
3. What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int a = 5; int b = ++a * a++; printf("%d ",b); return 0;
} </syntaxhighlight> (a) 25
(b) 30
(c) 36
(d) Undefined Behavior
4. What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int a = 5; switch(a) { default: a = 4; case 6: a--; case 5: a = a+1; case 1: a = a-1; } printf("%d \n",a);
return 0;
} </syntaxhighlight> (a) 5
(b) 4
(c) 3
(d) None of these
5. What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int a = 2,b = 5; a = a^b; b = b^a; printf("%d %d",a,b);
return 0;
} </syntaxhighlight> (a) 5 2
(b) 2 5
(c) 7 7
(d) 7 2
6. What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int a[][3] = {1, 2, 3, 4, 5, 6}; int (*ptr)[3] = a; printf("%d %d ", (*ptr)[1], (*ptr)[2]); ++ptr; printf("%d %d\n", (*ptr)[1], (*ptr)[2]);
return 0;
} </syntaxhighlight> (a) 2 3 5 6
(b) 2 3 4 5
(c) 4 5 0 0
(d) none of the above
7. What is the output of the following program?
<syntaxhighlight lang="c">
void f(char**);
int main() {
char *argv[] = { "ab", "cd", "ef", "gh", "ij", "kl" }; f(argv);
return 0;
} void f(char **p) {
char *t; t = (p += sizeof(int))[-1]; printf("%s\n", t);
} </syntaxhighlight> (a) ab
(b) cd
(c) ef
(d) gh
8. What is the output of the following program?
<syntaxhighlight lang="c">
int ripple(int n, ...) {
int i, j, k; va_list p; k = 0; j = 1; va_start(p, n); for (; j < n; ++j) { i = va_arg(p, int); k += i; } va_end(p); return k;
} int main() {
printf("%d\n", ripple(3, 5, 7));
return 0;
} </syntaxhighlight> (a) 12
(b) 5
(c) 7
(d) 15
9. What is the output of the following program?
<syntaxhighlight lang="c">
int counter(int i) {
static int count = 0; count = count + i; return count;
}
int main() {
int i, j; for (i = 0; i <= 5; i++) j = counter(i); printf("%d\n", j);
return 0;
} </syntaxhighlight> (a) 10
(b) 15
(c) 6
(d) 7
10. What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
const int x = 5; const int *ptrx; ptrx = &x; *ptrx = 10; printf("%d\n", x);
return 0;
} </syntaxhighlight> (a) 5
(b) 10
(c) Compile Error
(d) Garbage value
11. What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int i; i = x*x*x; printf("%d",i);
return 0;
} </syntaxhighlight> (a) 125
(b) 13
(c) 17
(d) None of above
12. What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
char c=125; c=c+10; printf("%d",c);
return 0;
} </syntaxhighlight> (a) 135
(b) +INF
(c) -121
(d) -8
13. What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int i=10; static int x=i; if(x==i) printf("Equal"); else if(x>i) printf("Greater"); else printf("Lesser");
return 0;
} </syntaxhighlight> (a) Equal
(b) Greater
(c) Lesser
(d) Compile Error
14. Consider the following code segment:
<syntaxhighlight lang="c">
int *f1() {
int x = 10; return &x;
} int *f2() {
int *ptr; *ptr = 10; return ptr;
} int *f3() {
int *ptr; ptr = (int*) malloc(sizeof (*ptr)); return ptr;
} </syntaxhighlight> Which of these functions uses pointers incorrectly?
(a) f3 only
(b) f1 and f3
(c) f1 and f2
(d) f1, f2, and f3
15. What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int i = 3; int j; j = sizeof(++i + ++i); printf("i=%d j=%d\n", i, j);
return 0;
} </syntaxhighlight> (a) i=4 j=4
(b) i=3 j=4
(c) i=5 j=4
(d) the behavior is undefined
16. What is the output of the following program?
<syntaxhighlight lang="c">
void f1(int*, int); void f2(int*, int); void (*p[2])(int*, int);
int main() { int a = 3; int b = 5; p[0] = f1; p[1] = f2; p[0](&a, b); printf("%d %d ", a, b); p[1](&a, b); printf("%d %d\n", a, b); return 0; }
void f1(int *p, int q) { int tmp = *p;
q = tmp; } void f2(int *p, int q) { int tmp = *p;
q = tmp; } </syntaxhighlight> (a) 5 5 5 5 (b) 3 5 3 5 (c) 5 3 3 5 (d) none of the above
17. What is the output of the following program? <syntaxhighlight lang="c">
void e(int);
int main() { int a = 3; e(a); putchar('\n'); return 0; } void e(int n) { if (n > 0) { e(--n); printf("%d ", n); e(--n); } } </syntaxhighlight> (a) 0 1 2 0 (b) 0 1 2 1 (c) 1 2 0 1 (d) 0 2 1 1
18. Consider the following code segment: <syntaxhighlight lang="c"> typedef int (*test)(float*, float*); test tmp; </syntaxhighlight> What is the type of tmp?
(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
19. What is the output of the following program? <syntaxhighlight lang="c">
int main() { char p; char buf[10] = {1, 2, 3, 4, 5, 6, 9, 8}; p = (buf + 1)[5]; printf("%d\n", p); return 0; } </syntaxhighlight> (a) 5 (b) 6 (c) 9 (d) none of the above
20. What is the output of the following program? <syntaxhighlight lang="c">
int main() { struct node { int a; int b; int c; }; struct node s = { 3, 5, 6 }; struct node *pt = &s; printf("%d\n", *((int*)pt+1)); return 0; } </syntaxhighlight> (a) 3 (b) 5 (c) 6 (d) 7
21. What is the output of the following program? <syntaxhighlight lang="c">
int main(void) { char a[5] = { 1, 2, 3, 4, 5 }; char *ptr = (char*)(&a + 1); printf("%d %d\n", *(a + 1), *(ptr - 1)); return 0; } </syntaxhighlight> (a) Compile Error (b) 2 1 (c) 2 5 (d) none of the above
22. What is the output of the following program? <syntaxhighlight lang="c">
void foo(int[][3]);
int main(void) { int a[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; foo(a); printf("%d\n", a[2][1]); return 0; }
void foo(int b[][3]) { ++b; b[1][1] = 9; } </syntaxhighlight> (a) 8 (b) 9 (c) 7 (d) none of the above
23. Consider the following function: <syntaxhighlight lang="c"> int foo(int x, int n) { int val = 1; if (n > 0) { if (n % 2 == 1) val *= x; val *= foo(x * x, n / 2); } return val; } </syntaxhighlight> What function of x and n is computed by foo? (a) x^n (b) x×n (c) nx (d) none of the above
24. What is the output of the following program? <syntaxhighlight lang="c">
int main() { int a = 0; switch(a) { default: a = 4; case 6: a--; case 5: a = a+1; case 1: a = a-1; }
printf("%d \n",a);
return 0; } </syntaxhighlight> (a) 5 (b) 4 (c) 3 (d) 0
25. What is the output of the following program? <syntaxhighlight lang="c">
int main() { int a = 2; if(a == (1,2)) printf("Hello"); if(a == 1,2) printf("World"); return 0; } </syntaxhighlight> (a) Hello (b) World (c) Hello World (d) Compile Error
26. What is the output of the following program? <syntaxhighlight lang="c">
int main() { int a = 1,2; int b = (1,2); if(a == b) printf("Equal"); else printf("Not Equal"); return 0; } </syntaxhighlight> (a) Equal (b) Not Equal (c) Compiler Dependent (d) Compile Error
27. What is the output of the following program? <syntaxhighlight lang="c">
void foo(char *);
int main() { char *string = "Hello"; foo(string); printf("%s",string); return 0; }
void foo(char *a) { while(*a) {
a++; } } </syntaxhighlight> (a) Hello (b) Ifmmp (c) Compile Error (d) Segmentation fault
28. What is the output of the following program? <syntaxhighlight lang="c">
int main() { char s[] = "Opendays2012"; int i = 0; while(*(s++)) i++; printf("%d",i); return 0; } </syntaxhighlight> (a) Segmentation Fault (b) Compile Error (c) 12 (d) 0
29. What is the output of the following program? <syntaxhighlight lang="c">
int a = 10; int main() { fun(); fun(); return 0; }
int fun() { static int a = 1; printf("%d ",a); a++; return 0; } </syntaxhighlight> (a) 1 2 (b) 1 1 (c) 10 11 (d) 10 10
30. What is the output of the following program? <syntaxhighlight lang="c">
int begin() { printf("Hello\n"); return 0; } </syntaxhighlight> (a) Hello (b) Link error (c) Segmentation fault (d) Compiler error
31. Consider the following program: <syntaxhighlight lang="c">
int main() { int a[10][20][30]={0}; printf("%ld",&a+1 - &a); return 0; } </syntaxhighlight> What is the output of this program? Ans:
32. Consider the following program:
<syntaxhighlight lang="c">
int main() { int a[10][20][30] = {0}; int *b = a; int *c = a+1; printf("%ld", c-b); return 0; } </syntaxhighlight> What is the output of this program? (You may ignore compiler warnings)
Ans:
33. Consider the following program:
<syntaxhighlight lang="c">
int* fun();
int main() { int *a = fun(); printf("%d",*a); return 0; } int* fun() { int *a =(int*) malloc(sizeof(int));
return a; } </syntaxhighlight> What is the output of this program? Ans:
34. Consider the following program:
<syntaxhighlight lang="c">
int main() { int *a = fun(); printf("%d",*a); return 0; } int fun() { int a = 10; return a; } </syntaxhighlight> What is the output of this program? Ans:
35. Consider the following program:
<syntaxhighlight lang="c">
int main() { char string[] = "Hello"; printf("%lu %lu",sizeof(string),strlen(string)); return 0; } </syntaxhighlight> What is the output of this program? Ans:
36. Consider the following program:
<syntaxhighlight lang="c">
int main() { float a = 0.5; if(a == 0.5) printf("Yes"); else printf("No"); return 0; } </syntaxhighlight> What is the output of this program? Ans:
37. Consider the following program:
<syntaxhighlight lang="c">
void foo(char *);
int main() { char a[100] = {0}; printf("%lu %lu",sizeof(a),strlen(a)); return 0; } </syntaxhighlight> What is the output of this program? Ans:
38. Consider the following program:
<syntaxhighlight lang="c">
int main() { int a; printf("%d",scanf("%d",&a)); return 0; } </syntaxhighlight> What is the output of the above code?
Ans:
39. 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 program ? <syntaxhighlight lang="c">
int main() { float a=5.375; char *p; int i; p = (char*)&a; for(i=0; i<2; i++) printf("%02x ", (unsigned char)(p[i]^p[3-i])); return 0; } </syntaxhighlight> Ans:
40. Consider the following program:
<syntaxhighlight lang="c">
int main() { char str[] = {'a','b','c','\0'}; str[0] -= 32; printf("%s",str); return 0; } </syntaxhighlight> What is the output of the above code?
Ans:
41. What is the following function doing?
<syntaxhighlight lang="c">
int foo(int n)
{
int sum = 0;
while(n > 0)
{
n = n & n-1;
sum++;
}
return sum;
}
</syntaxhighlight>
Ans:
42. What is the following function doing? <syntaxhighlight lang="c"> int foo(int a, int b) { int c = a, d = b; while(a != b) { if(a < b) a = a+c; else b = b+d; } return a; } </syntaxhighlight> Ans:
43. What is the following function doing? <syntaxhighlight lang="c"> int foo( int a, int b) { int c = a-b; c = c&(0x80000000); return (!c)*a +(!!c)*b; } </syntaxhighlight> Ans:
44. What is the following function doing? <syntaxhighlight lang="c"> unsigned fun(unsigned a, unsigned b) {
int i; unsigned j = 0; for(i = 0; i < 32; i++) { j <<= 1; j += !!(a & 0x80000000); a <<= 1; if(j >=b) { j -= b; a++; }
} return a;
} </syntaxhighlight> Ans:
45. What is the following function doing? <syntaxhighlight lang="c"> unsigned fun(unsigned int a) {
unsigned int i, x = 0, y = 0, z = 0; for(i = 0; i < 16; i++) { y <<= 2; y += !!(a & 0x80000000) << 1; y += !!(a & 0x40000000); a <<= 2; x = x + (x&1); x <<= 1; z <<= 1; if(x + 1 <= y) { x++; z++;
y-=x;
} } 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 “Hello World”. <syntaxhighlight lang="c">
int main() { if(<condition>) { printf("Hello "); } else { printf("World\n"); } 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: