Difficulty: Medium
Correct Answer: Value of x is 1
Explanation:
Introduction / Context:
This question tests your understanding of operator precedence and associativity in C, particularly for the modulus (%), multiplication (*) and division (/) operators. All three have the same precedence and are evaluated left to right. The result is then printed using printf.
Given Data / Assumptions:
• The expression assigned to x is 5 % 2 * 3 / 2.
• All operands are integers, so integer arithmetic is used.
• The variable y is unused and does not affect x.
• printf prints x with the format "Value of x is %d".
Concept / Approach:
Since %, * and / share the same precedence level in C, the expression is evaluated strictly from left to right. Each intermediate result is an integer, and division truncates toward zero. We can break the compound expression into sequential steps, computing two operands at a time, then using the result in the next operation. Carefully following this evaluation order yields the final integer value stored in x.
Step-by-Step Solution:
Step 1: Start with 5 % 2 * 3 / 2. Because of left to right associativity, compute 5 % 2 first.
Step 2: 5 % 2 is the remainder when 5 is divided by 2, which is 1.
Step 3: The expression now becomes 1 * 3 / 2.
Step 4: Next, compute 1 * 3, still following left to right order. That gives 3.
Step 5: The expression is now 3 / 2. As integer division, 3 / 2 is 1 because the fractional part 0.5 is discarded.
Step 6: Thus x is assigned the value 1. printf prints "Value of x is 1".
Verification / Alternative check:
You can confirm this result by quickly rewriting the expression with parentheses that reflect left to right associativity: ((5 % 2) * 3) / 2. Evaluating this form step by step clearly shows the same sequence: (5 % 2) = 1, then 1 * 3 = 3, then 3 / 2 = 1. This matches the earlier reasoning and confirms that any different result comes from misapplying precedence or treating division as floating point instead of integer.
Why Other Options Are Wrong:
Option B ("Value of x is 2") might come from mistakenly computing 3 / 2 as 1.5 and then rounding up, which is not how integer division behaves. Option C ("Value of x is 3") would be correct if the final division by 2 were forgotten, leaving x = 3. Option D ("Compile time error") is wrong because the expression is perfectly valid C, and unused variable y does not cause a compilation failure.
Common Pitfalls:
A common error is to assume that division of integers yields a floating point result, but in C the type of the operands controls the result type; integer division discards any fractional part. Another pitfall is misunderstanding precedence and associativity, for example trying to apply multiplication before modulus or incorrectly grouping operations. Remember that % has the same precedence as * and /, and all three associate from left to right, so evaluating them sequentially is the safest mental model.
Final Answer:
After evaluating 5 % 2 * 3 / 2 from left to right, the program prints "Value of x is 1".
Discussion & Comments