Exams Knowledge Hub

MCQs for Competitive Exams, School & College Exams

Module: | Core Architecture, Basics & Control Flow

Q14: Consider the following statements regarding operator precedence and associativity in Java:

1. In mathematical expressions lacking parentheses, multiplication and division operators will be evaluated prior to addition and subtraction due to their higher hierarchical precedence.
2. When an expression contains multiple operators that share the exact same precedence level, the Java compiler relies strictly on operator associativity to determine the direction of evaluation.
3. The assignment operator (=) features a standard left-to-right associativity, meaning a chained assignment like 'a = b = c = 10' assigns 10 to 'a' first, then 'b', then 'c'.

Which of the above statements is/are correct?
A
Only 1 and 2
B
Only 1 and 3
C
Only 2 and 3
D
1, 2, and 3
✅ Correct Answer: A
🎯 Quick Answer:
The correct combination is 1 and 2. Statement 3 is incorrect because assignment operators (=, +=, -=) possess a right-to-left associativity. In the chain 'a = b = c = 10', the value 10 is first assigned to 'c', the result of that is assigned to 'b', and finally to 'a'.
Concept Definition: Precedence defines the strict order in which different types of operators are processed in an expression.
Associativity acts as the tie-breaker rule, defining the direction (Left-to-Right or Right-to-Left) of evaluation when operators have identical precedence.
Structural Breakdown: The hierarchy generally places postfix/prefix unary operators at the top, followed by multiplicative (\*, /, %), additive (+, -), relational (\), equality (==, \!=), logical (&&, ||), and finally assignment operators at the absolute bottom.
Historical/Related Context: These evaluation rules are deeply rooted in standard algebraic conventions (BODMAS/PEMDAS) and the foundational design of the C programming language, upon which Java's syntax was heavily modeled to ensure familiarity for enterprise developers in the 1990s.
Causal Reasoning: If assignment operators were evaluated left-to-right, 'a = b = c = 10' would attempt to assign the current, potentially uninitialized value of 'b' into 'a', fundamentally breaking the logic of multi-variable initialization.
Right-to-left evaluation guarantees the concrete literal value propagates backward through the variable chain.