Both compile and run fine.
Consider `short s = 10; s = s + 5;` and `short t = 10; t += 5;`. Which statement is correct?
Only `s = s + 5` compiles.
Only `t += 5` compiles.
Both give compilation errors.
Show answer & explanationAnswer
Correct answer
Only `t += 5` compiles.
Explanation
`s = s + 5` causes a compilation error because `s + 5` is promoted to int, cannot assign int to short without cast. `t += 5` is a compound assignment that includes an implicit cast, so it compiles.
Written by ExamHoot EditorialPublished · Updated
All 10 Java -operators questions
- 1.Given the expression `int a = 5; int b = 10; int c = ++a * b-- + a;`, what is the value of `c` after evaluation?
- 2.What is the result of evaluating `(2 + 3) * 4 == 20 || !(5 > 3) && (6 / 2 == 3)` in Java?
- 3.Given `int x = 7; int y = 3; int z = x ^ y;` and then `z = z << 1;`, what is the final value of `z`?
- 4.Which of the following expressions evaluates to `true` for all non-negative integers `n`?
- 5.Consider `short s = 10; s = s + 5;` and `short t = 10; t += 5;`.
- 6.What is the output of the following code?
- 7.Given `int x = 5; x = x++ + ++x;`, what is the final value of `x`?
- 8.What is the value of `(int) (3.9 + 5.1 / 2.0)` in Java?
- 9.Given `boolean a = true; boolean b = false; boolean c = a | b & !a;`, what is the value of `c`?
- 10.Which of these expressions is guaranteed to produce arithmetic overflow?
