Multiple choice technology programming languages

which is the value of x and y after running this code? class a { public static void main(String args[]) { int x=1; int y=x++; } }

  1. x=1 and y=1

  2. x=1 and y=2

  3. x=2 and y=2

  4. x=2 and y=1

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

The post-increment operator x++ first returns the current value of x (which is 1), then increments x to 2. So y receives 1 (the original value), and x becomes 2 after the operation. This is a fundamental concept of post-increment vs pre-increment (++x) operators in Java.