3,013 views

In Java, after executing the following code what are the values of x, y and z?

int x,y=10; z=12;

x=y++ + z++;

1. x=22, y=10, z=12
2. x=24, y=10, z=12
3. x=24, y=11, z=13
4. x=22, y=11, z=13

Answer is [D]  x=22, y=11, z=13

Can you explain how x=22? Because increment operators has higher priority then '+'. operator. So x should be 24 here.

X and Y has postfix increment so increments will happen after their value has been used in expression.That is why x=10+12=22. after that X and Y will increment as X=11 and Y=13.

ans  is (d)

by
x = y++ + z++;

As in post increment operator, first the value is assigned and then it is incremented, this statement can be re-written as:

x = y + z;
y = y++;
z = z++;

So, the value of x = 10 + 12 = 22, y = 10 + 1 = 11 and z = 12 + 1 = 13.

Option (D) is correct.

1
3,035 views