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.
Pranali said:
1 decade ago
I am not understand how to convert (x+=1) become 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)
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)
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)
Mulu gebrehiwet said:
9 years ago
This question is very very important.
(3)
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)
Athini said:
8 years ago
What is the value of float and short and what is the sign +=?
(1)
Syed Fahd said:
8 years ago
Why (x+= (short) y) is becoming (x=+1)?
What does short mean?
What does short mean?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers