TCS
Company
Programming
Output
Predict the output of following code:
main()
{
int a=10,x;
x= a-- + ++a;
printf(“%d”,x);
}
1. 19
2. 20
3. 22
4. 23
Read Solution (Total 6)
-
- 20
as first a=10 then a dec by 1 i.e=9 then +(1+9)
so,10+10=20 - 6 years agoHelpfull: Yes(10) No(0)
- 20
while the compiler read a-- it would be 10 then the control goes to '+' arithematic operator so due to post increment the value of a becomes 9 , after that the compiler detects ++a which means value of a =10 so 10 + 10 = 20 - 6 years agoHelpfull: Yes(4) No(0)
- 22 is answer
X=a--+ ++a;
first pre increment execute
x=11+11
x=22 - 6 years agoHelpfull: Yes(2) No(9)
- 22
operator precendency is right to left - 6 years agoHelpfull: Yes(1) No(5)
- 20 is solution
- 6 years agoHelpfull: Yes(1) No(1)
- Answer=19
Where X=a--+++a will compile first for a--=9
Then add with 10 afterwards its print 19 and then it will increase the 10 to 11 - 5 years agoHelpfull: Yes(0) No(1)
TCS Other Question
Predict the output of following code:
main()
{
int x,a=10;
x=9*5+ 7/3 -6+a; //45+2-6+10 = 51 // 7/3 =2 int division
printf(“%d”,x);
}
1. 51
2. 51.5
3. 31
4. None of these
Predict the output of following code:
main()
{
int a=10,x=20;a=a++ + 10; // a = 10+10 first a is increment to 11 but
overwritten by 20
x=x++ + ++a; // x = 20 + 21 a is incremented first from 20 to 21
printf(“%d,%d”,a,x);
}
1. 22,43
2. 12,21
3. 10,20
4. 42,42