Friday, February 23, 2007

Interview Questions

Given Below are some of the interview questions I encoutered ,and I am putting it here for my future use..

1.What is the size of below structure if int is 4 bytes long?

struct {
int a;
char b;
int c;
};
Ans:4+4+4=12;

2.Why is it now 4+1+4=9?
Ans: Due to word Alignment and padding of char b which makes it size of 4 bytes.

3.How can you avoid padding.?
Ans:It depends on compiler.Some compiler eg Diab compiler. provide compiler directive such as "__packed__" to avoid padding
so
__packed__ struct
{
int a;
char b;
int c;
}A;

Now the size of A is 4+1+4=9;

4.aow can u determine whether the number is a multiple of 2;
Ans: The last bit (LSB) of the number should be 0(zero),