Skip to main content

Quiz

Trend Micro

Q: Giving a function "func". What is the return value of func(9999) ?

int func(int x) {
int countx = 0;
while(x) {
countx++;
x = x&(x-1);
}
return ++countx;
}

A: 9

Q: Write down the screen output of the following function.

union {
int i;
char x[2];
} a;

int main()
{
a.x[0] = 10;
a.x[1] = 1;
printf("%d", a.i); // 0001 0000 1010
}

A: 266

Q: Write down the screen output of the following function.

#include <stdio.h>

int a[3] = {-100, 8, 5};

void printarray(void) {
printf("a[0]=%d,a[1]=%d,a[2]=%d\n", a[0], a[1], a[2]);
}

int main(int argc, char *argv[])
{
int *ip = &(a[2]);
a[0] = a[2] - 2;
(*ip) += a[1] % 5;
printarray();
ip -= 2;
*ip += 5;
*(ip+1) /= 3;
printarray();
ip[1] = *(a+2) * 3;
a[2] = ip[0] - ip[2];
printarray();
}

A:

a[0]=3,a[1]=8,a[2]=8 a[0]=8,a[1]=2,a[2]=8 a[0]=8,a[1]=24,a[2]=0

Q: Give the following,

#define DOUBLE(x) x+x

int main()
{
double i = 6 * DOUBLE(6);
printf("%s", i);
}

A: 42

Q: The followings are statements in Windows NT 32-bit system,

char str[] = "abcd";
char *p = str;
int n = 10;
printf("%d,", sizeof(str));
printf("%d,", sizeof(p));
printf("%d", sizeof(n));

A: 5,8,4

Q: Write down the screen output of the following function in Windows NT 32-bit system,

int i = 5;
printf("%d, %d\n", sizeof(++i), i);

A: 4, 5

Q: Write a function ";" which prints the "binary format" of f. For example, func(3.1416) prints "01000000 01001001 00001111 11111001"(IEEE-754). (Assume sizeof(float) is equal to sizeof(long).)

A:

void func(float f) {
union {
float f;
uint32_t u;
} fu = { .f = f };

for(int i = sizeof(fu) * 8 - 1; i >= 0; i--)
printf("%d", (fu.u & (1 << i)) >> i);
}