C# Programming - Datatypes - Discussion
Discussion Forum : Datatypes - General Questions (Q.No. 8)
8.
What will be the output of the following code snippet when it is executed?
int x = 1;
float y = 1.1f;
short z = 1;
Console.WriteLine((float) x + y * z - (x += (short) y));
Discussion:
18 comments Page 2 of 2.
Gaytri Verma said:
1 decade ago
Its very difficult but help to build the logic.
Sri said:
1 decade ago
Since x=1
y=1.1
z=1
console.write.line(1.0+1.1*1-(x+=1))
1.0+1.1*1-(x=x+1))
1.0+1.1*1-(2)
1.0+1.1-2
2.1-2
0.1
output is 0.1.
y=1.1
z=1
console.write.line(1.0+1.1*1-(x+=1))
1.0+1.1*1-(x=x+1))
1.0+1.1*1-(2)
1.0+1.1-2
2.1-2
0.1
output is 0.1.
Shaila said:
1 decade ago
BODMAS:
(x += (short) y)=[x=x+y]=2 .............. A.
y * z
1.1f*1
1.1f
float(1.1+1) = 2.1 ................ B.
2.1-2=0.1.
(x += (short) y)=[x=x+y]=2 .............. A.
y * z
1.1f*1
1.1f
float(1.1+1) = 2.1 ................ B.
2.1-2=0.1.
Madhuri said:
1 decade ago
Thank you teju.
Teju said:
1 decade ago
Slight changes in sri kantha answer...
Since x=1
y=1.1
z=1
console.write.line(1.0+1.1*1-(x+=1))
1.0+1.1*1-(x=x+1))
1.0+1.1*1-(2)
1.0+1.1-2
2.1-2
0.1
output is 0.1
Since x=1
y=1.1
z=1
console.write.line(1.0+1.1*1-(x+=1))
1.0+1.1*1-(x=x+1))
1.0+1.1*1-(2)
1.0+1.1-2
2.1-2
0.1
output is 0.1
Ankush said:
1 decade ago
Float is converted to int. I think float should be converted to int.
Srikanth said:
1 decade ago
Since x=1
y=1.1
z=1
console.write.line(1.0+1.1*1-(x+=1))
1.0+1.1*1-(x=x+1+1))
1.0+1.1*1-(2)
1.0+1.1-2
2.1-2
0.1
output is 0.1
y=1.1
z=1
console.write.line(1.0+1.1*1-(x+=1))
1.0+1.1*1-(x=x+1+1))
1.0+1.1*1-(2)
1.0+1.1-2
2.1-2
0.1
output is 0.1
Monalisa Arya said:
1 decade ago
Please tell me how it is solved?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers