Multiple choice technology programming languages

int z,x=5,y=-10,a=4,b=2; z = x++ - --y * b / a; What number will z in the sample code above contain?

  1. 12

  2. 10

  3. 11

  4. 6

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Evaluating the expression: x++ is post-increment (use 5, then x becomes 6), --y is pre-decrement (y becomes -11), so the expression is 5 - (-11 * 2 / 4) = 5 - (-22/4) = 5 - (-5) = 10. In integer division, -22/4 = -5 (truncates toward zero). Final result: z = 10, x = 6, y = -11. Option B is correct.

AI explanation

To find the value of z in the given code, let's break down the expression step by step:

  1. x++ increments the value of x by 1 after it is used in the expression. So, the value of x is now 6.

  2. --y decrements the value of y by 1 before it is used in the expression. So, the value of y is now -11.

  3. y * b multiplies the value of y (-11) by the value of b (2), resulting in -22.

  4. -22 / a divides the value of -22 by the value of a (4), resulting in -5.5.

  5. Finally, z = 6 - (-5.5), which simplifies to z = 6 + 5.5. Therefore, z equals 11.5.

However, since the given variables are integers, the result will be truncated to the nearest integer. So, z will contain the value 11.

Therefore, the correct answer is option C) 11.