Pages

Ads 468x60px

Monday, November 17, 2008

c aptitude for placement / aptitude / interview/assessment/multiple choice/career/campus

c aptitude for placement / aptitude / interview/assessment/multiple choice/career/campus



1. main()
{
int x=20;
{
int x=10;
printf(“%dn”,x);
}
printf(“%dn”,x);
}

Ans: 10
20

2. main()
{
int x=5;
x = x++;
printf (“%dn”,x);
}

Ans: 5

3. main()
{
int x=10;
printf(“%d %d”, ++x, ++x)
}

Ans: 12 11

4. What is the output of the following program if its command line argument were …
Sample 1 5 7

main(int argc, char *argv[])
{
int x;
x= argv[1] + argv[2] +argv[3]);
printf (“%d”, x);
}

Ans: Will show error because compiler treats 1 5 7 as characters array

5. # define VOLDEMORT he_who_must_not_be_names
main()
{
printf(“VOLDEMORT”);
}

Ans VOLDEMORT


Find out the output of following:

1. main()
{
char string[]="Hello World";
display(string);
}
void display(char *string)
{
printf("%s",string);
}

Answer : Compiler Error : Type mismatch in redeclaration of function display
Explanation :
In third line, when the function display is encountered, the compiler doesn't know anything about the function display. It assumes the arguments and return types to be integers, (which is the default type). When it sees the actual function display, the arguments and type contradicts with what it has assumed previously. Hence a compile time error occurs.

2. main()
{
int m=- -2;
printf("m=%d",m);
}

Answer: m=2;
Explanation:
Here unary minus (or negation) operator is used twice.
maths rules applies, minus * minus= plus.


3. #define int char
main()
{
int k=65;
printf("sizeof(k)=%d",sizeof(k));
}

Answer: sizeof(k)=1
Explanation:
Since the #define replaces the string int by the macro char


4. main()
{
int m=10;
m=!m>14;
printf("m=%d",m);
}

Answer: m=0


5. #include
main()
{
char s[]={'a','b','c','\n','c','\0'};
char *q,*str,*str1;
q=&s[3];
str=q;
str1=s;
printf("%d",++*q + ++*str1-32);
}

Answer: 77
Explanation:
q is pointing to character '\n'. str1 is pointing to character 'a' ++*q. "q is pointing to '\n' and that is incremented by one." the ASCII value of '\n' is 10, which is then incremented to 11. The value of ++*q is 11. ++*str1, str1 is pointing to 'a' that is incremented by 1 and it becomes 'b'. ASCII value of 'b' is 98.
Now performing (11 + 98 – 32), we get 77("M"); So we get the output 77 :: "M" (Ascii is 77).




Predict the output or error(s) for the following:

1. void main()
{
int const * p=5;
printf("%d",++(*p));
}

Answer:
Compiler error: Cannot modify a constant value.
Explanation:
p is a pointer to a "constant integer". But we tried to change the value of the "constant integer".

2. main()
{
char s[ ]="man";
int i;
for(i=0;s[ i ];i++)
printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);
}

Answer:
mmmm
aaaa
nnnn
Explanation:
s[i], *(i+s), *(s+i), i[s] are all different ways of expressing the same idea. Generally array name is the base address for that array. Here s is the base address. i is the index number/displacement from the base address. So, indirecting it with * is same as s[i]. i[s] may be surprising. But in the case of C it is same as s[i].

3. main()
{
float me = 1.1;
double you = 1.1;
if(me==you)
printf("I love U");
else
printf("I hate U");
}

Answer:
I hate U
Explanation:
For floating point numbers (float, double, long double) the values cannot be predicted exactly. Depending on the number of bytes, the precession with of the value represented varies. Float takes 4 bytes and long double takes 10 bytes. So float stores 0.9 with less precision than long double.
Rule of Thumb:
Never compare or at-least be cautious when using floating point numbers with relational operators (== , >, <, <=, >=,!= ) .

4. main()
{
static int var = 5;
printf("%d ",var--);
if(var)
main();
}

Answer: 5 4 3 2 1

Explanation:When static storage class is given, it is initialized once. The change in the value of a static variable is retained even between the function calls. Main is also treated like any other ordinary function, which can be called recursively.

5. main()
{
int c[ ]={2.8,3.4,4,6.7,5};
int j,*p=c,*q=c;
for(j=0;j<5;j++) j="0;j<5;j++)" i="20;" i="-1,j="-1,k="0,l="2,m;" m="i++&&amp;amp;j++&&k++l++;" 0 =" 0)." i="3;" foo =" 123;int" of =" 0" i =" 0," len =" strlen(s);" str =" (char" err_num =" 2;return" p1="“name”;" p2="(char*)malloc(20);" x="20,y="35;" x="y++" y=" ++y" x="5;">>2);
}

Answer: 5,20,1

4. What will be printed as the result of the operation below:
#define swap(a,b) a=a+b;b=a-b;a=a-b;
void main()
{
int x=5, y=10;
swap (x,y);
printf(“%d %d\n”,x,y);
swap2(x,y);
printf(“%d %d\n”,x,y);
}
int swap2(int a, int b)
{
int temp;
temp=a;
b=a;
a=temp;
return 0;
}

Answer: 10, 510, 5

5. What will be printed as the result of the operation below:
main()
{
char *ptr = ” Cisco Systems”;
*ptr++;
printf(“%s\n”,ptr);
ptr++;
printf(“%s\n”,ptr);
}

Answer:Cisco Systemsisco systems

6. What will be printed as the result of the operation below:
main()
{
char s1[]=“Cisco”;
char s2[]= “systems”;
printf(“%s”,s1);
}

Answer: Cisco

7. What will be printed as the result of the operation below:
main()
{
char *p1;
char *p2;
p1=(char *)malloc(25);
p2=(char *)malloc(25);
strcpy(p1,”Cisco”); s
trcpy(p2,“systems”);
strcat(p1,p2);
printf(“%s”,p1);
}

Answer: Ciscosystems

8. The following variable is available in file1.c, who can access it?:
static int average;
Answer: all the functions in the file1.c can access the variable.

9. WHat will be the result of the following code?
#define TRUE 0 // some code
while(TRUE)
{
// some code
}

Answer: This will not go into the loop as TRUE is defined as 0.

10. What will be printed as the result of the operation below:
int x;
int modifyvalue()
{
return(x+=10);
}
int changevalue(int x)
{
return(x+=1);
}

void main()
{
int x=10;
x++;
changevalue(x);
x++;
modifyvalue();
printf("First output:%d\n",x);
x++;
changevalue(x);
printf("Second output:%d\n",x);
modifyvalue();
printf("Third output:%d\n",x);
}

Answer: 12 , 13 , 13

11. What will be printed as the result of the operation below:
main()
{
int x=10, y=15;
x = x++;
y = ++y;
printf(“%d %d\n”,x,y);
}

Answer: 11, 16

12. What will be printed as the result of the operation below:
main()
{
int a=0;
if(a==0) printf(“Cisco Systems\n”);
printf(“Cisco Systems\n”);
}

Answer: Two lines with “Cisco Systems” will be printed.

0 comments: