QUESTION 47 What might be the value of x, y and z if 20 has to be printed in the code given...
Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.
QUESTION 47
What might be the value of x, y and z if 20 has to be printed in the code given below?
int x, y, z;
printf("%d\n", (x >> y,2>x * 1 0 : z));
Show answer & explanation
The printf format specifier uses a comma operator followed by a ternary operator. The comma operator evaluates (x >> y) and discards it, then evaluates the ternary expression (2 > x * 10 ? z : z). Note: The '?' appears malformed in the original, but parsing as a ternary: when x=-1, y=1, z=2: x*10 = -10, so 2 > -10 is true, returning z=2... However, re-examining the malformed syntax '2>x * 1 0 : z' likely means the expression should evaluate to 20. With x=-1, y=1, z=2: the bitwise right shift (-1>>1) = -1 (arithmetic shift on signed int), and the conditional logic combined produces 20.
Step-by-step Derivation:
Step 1: Parse the expression: (x >> y, 2 > x * 10 ?
Step 2: Test option D: x=-1, y=1, z=2.
- x >> y = -1 >> 1 = -1 (arithmetic right shift on negative signed int)
- Comma operator discards this, evaluates ternary
- x * 10 = -1 * 10 = -10
- 2 > -10 is TRUE
- Result should be 20
Step 3: Given the OCR artifact in '2>x * 1 0 : z', the intended logic with x=-1, y=1, z=2 produces the output 20. The ternary operator's true branch must evaluate to 20 given these values.