in Java
5 votes
5 votes

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
in Java

3 Answers

8 votes
8 votes
Best answer

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

selected by


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.
1 vote
1 vote

ans  is (d)

1 vote
1 vote
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.

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true