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));
0.1
1.0
1.1
11
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
18 comments Page 1 of 2.

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.
(8)

Mulu gebrehiwet said:   9 years ago
This question is very very important.
(3)

Athini said:   8 years ago
What is the value of float and short and what is the sign +=?
(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.
(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.
(1)

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.
(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.

Syed Fahd said:   8 years ago
Why (x+= (short) y) is becoming (x=+1)?

What does short mean?

Pranali said:   1 decade ago
I am not understand how to convert (x+=1) become 2.

Monalisa Arya said:   1 decade ago
Please tell me how it is solved?


Post your comments here:

Your comments will be displayed after verification.