Multiple choice technology programming languages

Analyze the following code. public class Test { int x; public Test(String t) { System.out.println("Test"); } public static void main(String[] args) { Test test = null; System.out.println(test.x); } }

  1. The program has a syntax error because test is not initialized.

  2. The program has a syntax error because x has not been initialized.

  3. The program has a syntax error because you cannot create an object from the class that defines the object.

  4. The program has a syntax error because Test does not have a default constructor.

  5. The program has a runtime NullPointerException because test is null while executing test.x.

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

The code compiles without syntax errors. Declaring test as null is valid, and x defaults to 0. However, at runtime, attempting to access test.x when the reference test is null causes a NullPointerException. The other options are incorrect because there are no compile-time or syntax errors present in this program.