GAME -- Debug the Code !!

• Question

• Hi guys ,

lets start a Technical game ...! The Rules are Simple ...

Rules :

1] Languages used  :  c , c++ , java, vb [ only debugging kind ]

2] If one member post any question , u should give the answer .

3] If You answered a Question , then post another question for other members .

Saturday, September 15, 2007 6:01 AM

• Hey....i'd guess 22...? Am not very good at c/c++ so am not sure if there are some hidden runtime/compiletime errors... but s'far as i know... while computing;

sum=(i++)+(++i);
it will first inc the value of i to 11;
so;
sum=11+(++i);
and it will inc the value of i to 12 AFTER it assigns the value of (11+11) to sum.....ie.22.....plz do let me know if im wrong....!! im here to l;earn...!!!!!!
Vandana
Sunday, September 16, 2007 3:35 AM

All replies

• q1]

What will be the o/p of this ?

Code Snippet
void main()
{
int i=10,sum;
sum=(i++) + (++i);
printf("sum=%d",sum);
}

Saturday, September 15, 2007 6:02 AM
• Hey....i'd guess 22...? Am not very good at c/c++ so am not sure if there are some hidden runtime/compiletime errors... but s'far as i know... while computing;

sum=(i++)+(++i);
it will first inc the value of i to 11;
so;
sum=11+(++i);
and it will inc the value of i to 12 AFTER it assigns the value of (11+11) to sum.....ie.22.....plz do let me know if im wrong....!! im here to l;earn...!!!!!!
Vandana
Sunday, September 16, 2007 3:35 AM

But the actual answer depends upon the Compiler ...

Sunday, September 16, 2007 8:47 AM
•

the Next Qustion to debug !!

Code Snippet
main()
{
int a=0;
if(a && (a++ || a++));
printf("%d",a);
}

wat is the o/p ???
Sunday, September 16, 2007 8:50 AM
• hi guys no one knows the answer ah ??
Monday, September 17, 2007 6:07 AM
• output will be a=2

Monday, September 17, 2007 3:00 PM
• Since a++ is called twice

the o/p is 2

Monday, September 17, 2007 4:07 PM
• you are right Guys !!!

Tuesday, September 18, 2007 8:33 AM
• The Next One ::

Code Snippet

void main()
{

printf("ans=%d",1/0);

}

wats the o/p ...

Tuesday, September 18, 2007 1:33 PM
• I think the answer will be a Runtime error: Division by Zero.
Now, for the previous question, isn't the answer 0? Since C follows lazy evaluation, it won't evaluate the right side of && once it finds that a is 0. This is because 'a' being 0, the && is anyway going to result in false. So C saves time by not evaluating right side of &&. So, the increments never happen, and 'a' remains unmodified...
I tried the code in Dev-C++ too. Only 0 was printed in this case. Any others got different answers?
Tuesday, September 18, 2007 2:42 PM
•

Next one !!!

Give the output of the program :

void main()
{
char *s="12345sn";
printf("%d",sizeof(s));
}

Friday, September 21, 2007 5:27 AM

Friday, September 21, 2007 8:31 AM
• Sorry but you have got to be kidding me. All these quesitons are basically like 1+1 in the programming world. I had more difficult questions in my +2 (that is saying something). Some more competitive questions please? I love debugging

Friday, September 21, 2007 7:28 PM
• ok rahul .... This is for you !!

Code Snippet
/* Mult.c */

int main ( int argc, char* argv[ ] )
{
int a[3], i, ret ;
if ( argc < 3 || argc > 3 )
{
printf ( "No arg...blah blah ") ;
exit ( 0 ) ;
}

for ( i = 1 ; i < argc ; i++ )
a = atoi ( argv ) ;
ret = a[1] * a[2] ;
return ret ;
}

Code Snippet

/* Spawn.c */

#include
#include

main( )
{
int val ;
val = spawnl ( P_WAIT, "C:\\Mult.exe", "3", "10",
"20", NULL ) ;
printf ( "\nReturned value is: %d", val ) ;
}

Explain wats the o/p ... and how ??
Saturday, September 22, 2007 2:51 AM
•

wats the output for this !!
Friday, October 19, 2007 3:07 AM
• A new challenge

Display the non Fibonacci series

Ex- 4 6 7 9 10 11 12 ..........................

Friday, October 19, 2007 8:02 AM