Tuesday 19 March 2013

p="%d\n";


    main()
{
char *p;
p="%d\n";
             p++;
             p++;
             printf(p-2,300);
}
Answer:
    300
Explanation:
The pointer points to % since it is incremented twice and again decremented by 2, it points to '%d\n' and 300 is printed


Blog Author: Vijay Kumar

Go to: Java Aptitude


for(i=1;i>-2;i--)


    main(){
 unsigned int i;
 for(i=1;i>-2;i--)
             printf("c aptitude");
}
Explanation:
i is an unsigned integer. It is compared with a signed value. Since the both types doesn't match, signed is promoted to unsigned value. The unsigned equivalent of -2 is a huge value so condition becomes false and control comes out of the loop.


Blog Author: Vijay Kumar

Go to: Java Aptitude

if(a,b,x,y)


    main(){
  int a= 0;int b = 20;char x =1;char y =10;
  if(a,b,x,y)
        printf("hello");
 }
Answer:
hello
Explanation:
The comma operator has associativity from left to right. Only the rightmost value is returned and the other values are evaluated and ignored. Thus the value of last variable y is returned to check in if. Since it is a non zero value if becomes true so, "hello" will be printed.


Blog Author: Vijay Kumar

Go to: Java Aptitude


i = abc();


main()
{
 int i;
 i = abc();
 printf("%d",i);
}
abc()
{
 _AX = 1000;
}
Answer:
1000
Explanation:
Normally the return value from the function is through the information from the accumulator. Here _AH is the pseudo global variable denoting the accumulator. Hence, the value of the accumulator is set 1000 so the function returns value 1000.


Blog Author: Vijay Kumar

Go to: Java Aptitude

if(i && j++)


main()
{
 int i =0;j=0;
 if(i && j++)
   printf("%d..%d",i++,j);
printf("%d..%d,i,j);
}
Answer:
0..0
Explanation:
The value of i is 0 i.e. false for if. Since this information is enough to determine the truth value of the boolean expression. So the statement following the if statement is not executed.  The values of i and j remain unchanged and get printed.


Blog Author: Vijay Kumar

Go to: Java Aptitude


FILE *ptr;


    #include<stdio.h>
main()
{
FILE *ptr;
char i;
ptr=fopen("zzz.c","r");
while((i=fgetch(ptr))!=EOF)
printf("%c",i);
}
Answer:
contents of zzz.c followed by an infinite loop 
    Explanation:
The condition is checked against EOF, it should be checked against NULL.


Blog Author: Vijay Kumar

Go to: Java Aptitude


int one_d[]={1,2,3};


    # include <stdio.h>
int one_d[]={1,2,3};
main()
{
 int *ptr;
 ptr=one_d;
 ptr+=3;
 printf("%d",*ptr);
}
Answer:
garbage value
Explanation:
ptr pointer is pointing to out of the array range of one_d.

Blog Author: Vijay Kumar


Go to: Java Aptitude


main(int argc, char **argv)


    main(int argc, char **argv)
{
 printf("enter the character");
 getchar();
 sum(argv[1],argv[2]);
}
sum(num1,num2)
int num1,num2;
{
 return num1+num2;
}
Answer:
Compiler error.
Explanation:
argv[1] & argv[2] are strings. They are passed to the function sum without converting it to integer values. 


Blog Author: Vijay Kumar

Go to: Java Aptitude

int i=_l_abc(10);


    main()
{
 int i=_l_abc(10);
     printf("%d\n",--i);
}
int _l_abc(int i)
{
 return(i++);
}
Answer:
9
Explanation:
return(i++) it will first return i and then increments. i.e. 10 will be returned.


Blog Author: Vijay Kumar

Go to: Java Aptitude


printf("%d",i+++j);


    main()
{
      int i=5,j=6,z;
      printf("%d",i+++j);
     }
Answer:
11
Explanation:
the expression i+++j is treated as (i++ + j)   


Blog Author: Vijay Kumar

Go to: Java Aptitude