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 1 of 2.
Vinod said:
1 decade ago
Given question is,
int x = 1;
float y = 1.1f;
short z = 1;
Console.WriteLine((float) x + y * z - (x += (short) y)).
Now solution,
Here, (float)x = 1.0 because given that float and x=1 then finally use in left x value is 1.0.
As same as in (short) y ,given y=1. 1f but in this place we use only y=1(due to short).
Now,
Console.write.line(1.0+1.1*1-(x+=1)).
1.0+1.1*1-(x=x+1)) // (x+=1) = (x=x+1) here given value of x=1.
Then after calculation in this place x=1+1=2.
1.0+1.1*1-(2).
1.0+1.1-2.
2.1-2.
0.1.
Output is 0.1.
int x = 1;
float y = 1.1f;
short z = 1;
Console.WriteLine((float) x + y * z - (x += (short) y)).
Now solution,
Here, (float)x = 1.0 because given that float and x=1 then finally use in left x value is 1.0.
As same as in (short) y ,given y=1. 1f but in this place we use only y=1(due to short).
Now,
Console.write.line(1.0+1.1*1-(x+=1)).
1.0+1.1*1-(x=x+1)) // (x+=1) = (x=x+1) here given value of x=1.
Then after calculation in this place x=1+1=2.
1.0+1.1*1-(2).
1.0+1.1-2.
2.1-2.
0.1.
Output is 0.1.
(1)
Alok Binwal said:
1 decade ago
int x = 1;
float y = 1.1f;
short z = 1;
Console.WriteLine((float) x + y * z - (x += (short) y));
Solution:
float(x+(y*z)-(x=x+(sort)y)).
float(1+(1.1*1)-(x=1+1)).
float(1+1.1-2).
float(2.1-2).
float(0.1).
0.1.
float y = 1.1f;
short z = 1;
Console.WriteLine((float) x + y * z - (x += (short) y));
Solution:
float(x+(y*z)-(x=x+(sort)y)).
float(1+(1.1*1)-(x=1+1)).
float(1+1.1-2).
float(2.1-2).
float(0.1).
0.1.
(1)
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
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
Eduardo said:
1 decade ago
int x = 1;
float y = 1.1f;
short z = 1;
x + y * z - (x += (short) y));
Multiplication solves first.
= 1 + (1.1*1) - (1+1).
= 1 + 1.1 - 2.
= 2.1 - 2.
= 0.1.
float y = 1.1f;
short z = 1;
x + y * z - (x += (short) y));
Multiplication solves first.
= 1 + (1.1*1) - (1+1).
= 1 + 1.1 - 2.
= 2.1 - 2.
= 0.1.
(1)
Ajesh.G said:
1 decade ago
In this program x has value 1, so
x+=1;
x=x+1;
x=1+1;
Now the value(1+1=2)is assigned to x and the value x at this point changes to 2.
x+=1;
x=x+1;
x=1+1;
Now the value(1+1=2)is assigned to x and the value x at this point changes to 2.
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.
Purnima said:
8 years ago
((float) x + y * z - (x += (short) y)).
1+1.1*1-(x=+1)
1+1.1*1-(2)
1+1.1-2
2.1-2.0
0.1.
1+1.1*1-(x=+1)
1+1.1*1-(2)
1+1.1-2
2.1-2.0
0.1.
(8)
Ankush said:
1 decade ago
Float is converted to int. I think float should be converted to int.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers