Java Programming - Operators and Assignments - Discussion
Discussion Forum : Operators and Assignments - Pointing out the correct statements (Q.No. 2)
2.
Which of the following are legal lines of code?
- int w = (int)888.8;
- byte x = (byte)1000L;
- long y = (byte)100;
- byte z = (byte)100L;
Answer: Option
Explanation:
Statements (1), (2), (3), and (4) are correct. (1) is correct because when a floating-point number (a double in this case) is cast to an int, it simply loses the digits after the decimal.
(2) and (4) are correct because a long can be cast into a byte. If the long is over 127, it loses its most significant (leftmost) bits.
(3) actually works, even though a cast is not necessary, because a long can store a byte.
Discussion:
1 comments Page 1 of 1.
Abc said:
8 years ago
If there is a loss of precision their would be compile time error stating loss of precision. Then how its legal?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers