Telegram Group & Telegram Channel
What is the output of the following code?

3: byte a = 40, b = 50;
4: byte sum = (byte) a + b;
5: System.out.println(sum);


A. 40
B. 50
C. 90
D. The code will not compile because of line 4.
E. An undefined value

Explanation:
Line 4 generates a possible loss of precision compiler error. The cast operator has the highest precedence, so it is evaluated first, casting a to a byte. Then, the addition is evaluated, causing both a and b to be promoted to int values. The value 90 is an int and cannot be assigned to the byte sum without an explicit cast, so the code does not compile. The code could be corrected with parentheses around (a + b), in which case option C would be the correct answer.



tg-me.com/topJavaQuizExplain/38
Create:
Last Update:

What is the output of the following code?


3: byte a = 40, b = 50;
4: byte sum = (byte) a + b;
5: System.out.println(sum);


A. 40
B. 50
C. 90
D. The code will not compile because of line 4.
E. An undefined value

Explanation:
Line 4 generates a possible loss of precision compiler error. The cast operator has the highest precedence, so it is evaluated first, casting a to a byte. Then, the addition is evaluated, causing both a and b to be promoted to int values. The value 90 is an int and cannot be assigned to the byte sum without an explicit cast, so the code does not compile. The code could be corrected with parentheses around (a + b), in which case option C would be the correct answer.

BY Explanations “Top Java Quiz Questions”


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/topJavaQuizExplain/38

View MORE
Open in Telegram


telegram Telegram | DID YOU KNOW?

Date: |

Tata Power whose core business is to generate, transmit and distribute electricity has made no money to investors in the last one decade. That is a big blunder considering it is one of the largest power generation companies in the country. One of the reasons is the company's huge debt levels which stood at ₹43,559 crore at the end of March 2021 compared to the company’s market capitalisation of ₹44,447 crore.

telegram from cn


Telegram Explanations “Top Java Quiz Questions”
FROM USA