Multiple choice technology programming languages

What will happen when you compile and run the following code? public class MyClass{ static int i; public static void main(String argv[]){ System.out.println(i); } }

  1. Error Variable i may not have been initialized

  2. null

  3. 1

  4. 0

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

In Java, static (class) variables are automatically initialized to default values. For int, the default value is 0. Therefore, 'i' will be 0 when printed, and there will be no compilation error about uninitialized variables.

AI explanation

i is declared as a static int field without an explicit initializer, and in Java, uninitialized numeric fields (instance or static) are automatically given a default value of 0. This differs from local variables, which must be explicitly initialized before use or the compiler throws an error — that rule only applies to local variables, not fields, so there's no compile error here.