Multiple choice technology programming languages

What will happen when you attempt to compile and run the following code? public class Static { static { int x = 5; } static int x,y; public static void main(String args[]) { x--; myMethod(); System.out.println(x + y + ++x); } public static void myMethod() { y = x++ + ++x; } }

  1. Compile-time error

  2. prints : 3

  3. prints : 1

  4. prints : 7

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

Initially, static variable x is 0. x-- decrements it to -1. In myMethod(), y = x++ + ++x evaluates as -1 + 1 = 0, leaving x at 1. Back in main, x + y + ++x evaluates to 1 + 0 + 2 = 3.