Difficulty: Easy
Correct Answer: All statements are correct.
Explanation:
Introduction / Context:
Java allows explicit narrowing primitive conversions even when information will be lost. This question asks which given assignments compile; the focus is legality (compilation), not value correctness at runtime.
Given Data / Assumptions:
Concept / Approach:
Narrowing conversions (e.g., double → int, long → byte) require an explicit cast. Widening conversions (e.g., byte → long) are automatic. When an explicit cast is provided, the compiler accepts the conversion, even if the resulting value wraps or truncates.
Step-by-Step Solution:
Verification / Alternative check:
Remove the casts in lines 2 and 4 and compilation fails (possible lossy conversion). Keeping casts ensures success.
Why Other Options Are Wrong:
Common Pitfalls:
Assuming the compiler rejects narrowing even with a cast; in fact it compiles but may change the runtime value.
Final Answer:
All statements are correct.
Discussion & Comments