Java operators — which two expressions are equivalent among the following? 1) 3/2 2) 3<2 3) 34 4) 3<<2 Assume integer arithmetic and Java precedence.
-
A1 and 2
-
B2 and 3
-
C3 and 4
-
D1 and 4
Answer
Correct Answer: 3 and 4
Explanation
Introduction / Context:This question compares basic arithmetic with shift operations to see which yield the same result for constant integers, reinforcing that left shift by n multiplies by 2^n for non-overflowing operands.
Given Data / Assumptions:
- All literals are int.
- Left shift by 2 equals multiply by 4 when no overflow occurs.
Concept / Approach:Evaluate each expression: integer division truncates, comparison yields boolean, multiplication is straightforward, and left shift scales by a power of two. Compare the numeric outcomes for equality.
Step-by-Step Solution:
1) 3/2 → integer division → 1.2) 3<2 → boolean false (not an int value).3) 34 → 12.4) 3<<2 → 3 * 2^2 = 12.Thus, 3 and 4 are equivalent.Verification / Alternative check:Binary form: 3 is 0b0011; shifting left by 2 → 0b1100 = 12, matching 3*4.
Why Other Options Are Wrong:
- Options with 1 or 2 cannot equal 12; 1 is 1 and 2 is boolean.
Common Pitfalls:Forgetting that << is arithmetic scaling for non-overflowing positive ints; also mixing boolean and numeric expressions.
Final Answer:3 and 4