Pages

Friday

C Aptitude Questions with Answers Part 24

1)
void main() {
if(~0 == (unsigned int)-1) 
printf(“You  can  answer  this  if  you  know  how  values  are  represented  in 
memory”); 
} 

Answer
You can answer this if you know how values are represented in memory 
Explanation
~ (tilde operator or bit-wise negation operator) operates on 0 to produce all ones to fill the space for an integer. –1 is represented in unsigned value as all 1’s and so both are equal.

2)
int swap(int *a,int *b) {
 *a=*a+*b;*b=*a-*b;*a=*a-*b; 
} 
main() {
    int x=10,y=20; 
    swap(&x,&y); 
    printf("x= %d y = %d\n",x,y); 
} 

Answer
x = 20 y = 10
Explanation
This is one way of swapping two values. Simple checking will help understand this.

3)
main() {  
char *p = “ayqm”; 
printf(“%c”,++*(p++)); 
} 

Answer:
b

4)
main() {
   int i=5; 
   printf("%d",++i++); 
} 

Answer:
Compiler error: Lvalue required in function main
Explanation:
++i yields an rvalue. For postfix ++ to operate an lvalue is required.

5)
main() {
   char *p = “ayqm”; 
   char c; 
   c = ++*p++; 
   printf(“%c”,c); 
} 

Answer:
b
Explanation:
There is no difference between the expression ++*(p++) and ++*p++. Parenthesis just works as a visual clue for the reader to see which expression is first evaluated.

No comments:

Post a Comment