TEZPUR 2025 — Computer PYQ
TEZPUR | Computer | 2025What is the final value of x printed?
int x = 1;
if (x && --x) {
x += 2;
}
printf("%d", x);
Choose the correct answer:
- A.
0
- B.
1
(Correct Answer) - C.
5
- D.
4
1
Explanation
The correct answer is (b) 1.
To understand this result, we must analyze the behavior of the if condition using short-circuit evaluation and the decrement operator (--).
Logical AND (
&&) Short-Circuiting: The&&operator evaluates from left to right. If the left operand is false (0), the entire expression becomes false, and the right operand is not evaluated.Step-by-Step Execution:
Initialize
x = 1.Evaluate the
ifcondition:(x && --x).The first part,
x, is1, which istruein C.Because the left side is
true, the compiler must evaluate the right side,--x.The decrement operator
--x(pre-decrement) reducesxby 1 immediately. So,xbecomes0.The condition now effectively evaluates
(1 && 0), which is0(false).
Final Outcome:
Since the
ifcondition resulted in0(false), the code blockx += 2;is skipped.The value of
xremains0after the decrement, but wait—let's re-verify the post-evaluation.Actually,
xwas1, then--xturned it to0. The condition is false. The code exits theifblock.xis currently0. Theprintfwill print0.
Correction: Looking at the code: x starts at 1. if(1 && 0) is 0. The if body is not executed. x is 0. The answer is (a) 0.
We can represent the logical operation as:
Condition=x∧(–x)
Given x=1:
1∧0=0
Since the condition is false, the assignment x += 2 never occurs. Thus, the final value of x is:
xfinal=0
