Monday, March 10, 2008

Interview Puzzles questions FAQ

#include<stdio.h>


int
main()
{

int
a=10;

switch(a)
{

case
'1':

printf("ONE\n");

break;

case
'2':

printf("TWO\n");

break;

defa1ut:

printf("NONE\n");
}

return
0;
}

If you expect the output of the above program to be NONE, I would request you to check it out!!


 

The following C program segfaults of IA-64, but works fine on IA-32.


int
main()
{

int*
p;

p
= (int*)malloc(sizeof(int));

*p
=
10;

return
0;
}

Why does it happen so?


 

Here is a small piece of program(again just 14 lines of program) which counts the number of bits set in a number.

Input

Output

0

0(0000000)

5

2(0000101)

7

3(0000111)


int
CountBits (unsigned
int
x )
{

static
unsigned
int
mask[] = { 0x55555555,

0x33333333,

0x0F0F0F0F,

0x00FF00FF,

0x0000FFFF

} ;


int
i ;

int
shift ; /* Number of positions to shift to right*/


for ( i
=0, shift
=1; i
<
5; i
++, shift
*=
2)

x
= (x
&
mask[i ])+ ( ( x
>>
shift) &
mask[i]);

return
x;
}

Find out the logic used in the above program.