What is the output of code? oid main () { int x = 33, y, z; z = x++ + 17; y =…
2021
What is the output of code? oid main () { int x = 33, y, z; z = x++ + 17; y = ++z - ++x; cout << "x=" << x << " y=" << y << " z=" << z; }
- A.
x = 34 y = 17 z = 51
- B.
x = 35 y = 16 z = 51
- C.
x = 34 y = 16 z = 50
- D.
x = 35 y = 17 z = 51
Attempted by 140 students.
Show answer & explanation
Correct answer: B
Initial values:
x = 33Step 1:
z = x++ + 17;x++ is post-increment:
First use current value ofx
Then incrementx
So:
z = 33 + 17 = 50After execution:
x = 34
z = 50Step 2:
y = ++z - ++x;++z is pre-increment:
z = 51++x is pre-increment:
x = 35Now:
y = 51 - 35 = 16Final values:
x = 35
y = 16
z = 51Therefore, the output is:
x = 35 y = 16 z = 51