Difficulty: Easy
Correct Answer: double
Explanation:
Introduction / Context:
Expression type promotion in Java determines the resulting type of arithmetic expressions. Choosing a compatible return type requires analyzing how primitives are promoted.
Given Data / Assumptions:
x
is a byte, explicitly cast to long.y
is a double.(long)x / y * 2
.
Concept / Approach:
Division involving a double promotes the other operand to double, and the result is double. Multiplying a double by an int literal 2 yields a double. Therefore, the expression overall is of type double, and the return type must be compatible (double or wider in the sense of assignment compatibility; here the precise primitive type is double).
Step-by-Step Solution:
(long)x
→ long.Operation: long / double
→ double.Operation: double * 2
→ double.Conclusion: method must return double
.
Verification / Alternative check:
Changing return type to double
compiles; using long
or int
requires an explicit cast (and loses precision).
Why Other Options Are Wrong:int
, byte
, and long
are narrower than double
for this expression; Number
is not a primitive and does not match the expression type without boxing and is not a valid primitive return type here.
Common Pitfalls:
Assuming the initial cast to long dictates the overall type; forgetting that any double operand dominates.
Final Answer:
double
Discussion & Comments