What will be the value of i for the following expression? int f = 11, i = 3; i += (f > 3) ? i & 2 : 5;
Since f is 11, the condition f > 3 evaluates to true (1), so the conditional expression (f > 3) ? i & 2 : 5 selects and evaluates the second operand, i & 2. In binary, i = 3…
Choose your answer
What will be the value of i for the following expression?
int f = 11, i = 3; i += (f > 3) ? i & 2 : 5;
Answer and explanation
Correct answer B
Since f is 11, the condition f > 3 evaluates to true (1), so the conditional expression (f > 3) ? i & 2 : 5 selects and evaluates the second operand, i & 2. In binary, i = 3 is 0011 and 2 is 0010, so the bitwise AND operation 3 & 2 produces 0010, which is 2 in decimal. Finally, the compound assignment i += 2 adds 2 to the initial value of i (3 + 2), leaving i equal to 5.
Practice for these exams
-
UGC NET Computer Science and Applications