1)
main()
{
int a=10,*j;
void *k;
j=k=&a;
j++;
k++;
printf("\n %u %u ",j,k);
}
Answer:
Compiler error: Cannot increment a void pointer
Explanation:
            Void pointers are generic pointers and they can be used only when the type is not known and as an intermediate address storage type. No pointer arithmetic can be done on it and you cannot apply indirection operator (*) on void pointers.
2)
main()
{
char a[4]="HELL";
printf("%s",a);
}
Answer:
HELL%@!~@!@???@~~!
Explanation:
            The character array has the memory just enough to hold the string “HELL” and doesn’t have enough space to store the terminating null character. So it prints the HELL correctly and continues to print garbage values till it accidentally comes across a NULL character.
3)
Is this code legal?
int *ptr;
ptr = (int *) 0x400;
Answer:
Yes
Explanation:
            The pointer ptr will point at the integer in the memory location 0x400.
4)
void main()
{
printf(“sizeof (void *) = %d \n“, sizeof( void *));
printf(“sizeof (int *) = %d \n”, sizeof(int *));
printf(“sizeof (double *) = %d \n”, sizeof(double *));
printf(“sizeof(struct unknown *) = %d \n”, sizeof(struct unknown *));
}
Answer :
sizeof (void *) = 2
sizeof (int *) = 2
sizeof (double *) = 2
sizeof(struct unknown *) = 2
Explanation:
            The pointer to any type is of same size.
5)
main()
{
static int a[3][3]={1,2,3,4,5,6,7,8,9};
int i,j;
static *p[]={a,a+1,a+2};
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d\t%d\t%d\t%d\n",*(*(p+i)+j),
*(*(j+p)+i),*(*(i+p)+j),*(*(p+j)+i));
}
}
Answer:
1 1 1 1
2 4 2 4
3 7 3 7
4 2 4 2
5 5 5 5
6 8 6 8
7 3 7 3
8 6 8 6
9 9 9 9
Explanation:
            *(*(p+i)+j) is equivalent to p[i][j].
6)
main()
{
float i=1.5;
switch(i)
{
case 1: printf("1");
case 2: printf("2");
default : printf("0");
}
}
Answer:Compiler Error: switch expression not integral
Explanation:
            Switch statements can be applied only to integral types.
7)
main()
{
register int a=2;
printf("Address of a = %d",&a);
printf("Value of a = %d",a);
}
Answer:Compier Error: '&' on register variable
Rule to Remember:
            & (address of ) operator cannot be applied on register variables.
8)
main()
{
int i=5,j=10;
i=i&=j&&10;
printf("%d %d",i,j);
}
Answer:
1 10
Explanation:
            The expression can be written as i=(i&=(j&&10)); The inner expression (j&&10) evaluates to 1 because j==10. i is 5. i = 5&1 is 1. Hence the result.
9)
main()
{
int i=10,j=20;
j = i, j?(i,j)?i:j:j;
printf("%d %d",i,j);
}
Answer:
10 10
Explanation:
            The Ternary operator ( ? : ) is equivalent for if-then-else statement. So the question can be written as:
if(i,j)
{
if(i,j)
j = i;
else
j = j;
}
else
j = j;
10)
void main()
{
static int i;
while(i<=10)
(i>2)?i++:i--;
printf(“%d”, i);
}
Answer:
32767
Explanation:
            Since i is static it is initialized to 0. Inside the while loop the conditional operator evaluates to false, executing i--. This continues till the integer value rotates to positive value (32767).The while condition becomes false and hence, comes out of the while loop, printing the i value.
11)
main()
{
int i = 3;
for (;i++=0;) printf(“%d”,i);
}
Answer:
Compiler Error: Lvalue required.
Explanation:
            As we know that increment operators return rvalues and hence it cannot appear on
the left hand side of an assignment operation.
12)
main()
{
while (strcmp(“some”,”some%%CONTENT%%”))
printf(“Strings are not equal\n”);
}
Answer:No output
Explanation:
            Ending the string constant with %%CONTENT%% explicitly makes no difference. So “some” and “some%%CONTENT%%” are equivalent. So, strcmp returns 0 (false) hence breaking out of the while loop.
13)
main()
{ int i=5;
printf(“%d”,i=++i ==6);
}
Answer:1
Explanation:
            The expression can be treated as i = (++i==6), because == is of higher precedence than = operator. In the inner expression, ++i is equal to 6 yielding true (1). Hence the result.
14)
main()
{
unsigned int i=10;
while(i-->=0)
printf("%u ",i);
}
Answer:
10 9 8 7 6 5 4 3 2 1 0 65535 65534…..
Explanation:
            Since i is an unsigned integer it can never become negative. So the expression i-- >=0 will always be true, leading to an infinite loop.
15)
void main()
{
static int i=i++, j=j++, k=k++;
printf(“i = %d j = %d k = %d”, i, j, k);
}
Answer:
i = 1 j = 1 k = 1
Explanation:
            Since static variables are initialized to zero by default.