TCS
Company
Programming
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
Read Solution (Total 4)
-
- 21,41 as first value will be incremented by 1 (from 10 to 11) then 10 is added to it that's why value of a will be 21.
X=20+21 that's why value of x will be 41 - 6 years agoHelpfull: Yes(6) No(0)
- firstly a=10, x=20;
a=a++(10) + 1= 20 , but a++,so a is postfix->a=20+1=21
x=x++(20) + ++a(21+1)=42, so x is also here postfix->42+1=43 - 6 years agoHelpfull: Yes(1) No(0)
- 22,43 1st a++ +10=a=21,x=x++ + ++a=41,but there is post increment so after that a became 22 and k become 43
- 6 years agoHelpfull: Yes(1) No(0)
- a=10
a=11+10=21
x=20+22=42
x increased 1 time so x=43
So ans:a=22,x=43 - 6 years agoHelpfull: Yes(1) No(0)
TCS Other Question
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
Predict the output of following code:
main()
{
int i=10,j=2,k=0,m;
m=++i&&++j&&++k; // m = 11 && 3 && 1 = 1
printf(“%d%d%d%d”,i,j,k,m);
}
1.11,3,1,1
2. 11,2,0,1
3. 11,3,1,0
4. 10,2,0,1