1Z0-803 · Question #255
Given: package p1; public class Test { static double dvalue; static Test ref; public static void main(String[] args) { System.out.println(ref); System.out.println(dvalue); } } What is the result?
The correct answer is C. Null. Static reference type fields in Java are automatically initialized to null if no explicit initialization is provided. Static primitive type fields are initialized to their respective default values.
Question
Given:
package p1; public class Test { static double dvalue; static Test ref; public static void main(String[] args) { System.out.println(ref); System.out.println(dvalue); } } What is the result?
Options
- Ap1.Test.class
- B<the summary address refrenced by ref>
- CNull
- DCompilation fails
- EA NullPointerException is thrown at runtime
How the community answered
(35 responses)- A3% (1)
- B3% (1)
- C86% (30)
- D9% (3)
Why each option
Static reference type fields in Java are automatically initialized to `null` if no explicit initialization is provided. Static primitive type fields are initialized to their respective default values.
Incorrect, `ref` is a reference to an object of type `Test`, not the `Class` object itself.
Incorrect, `System.out.println()` for a null reference prints the string "null", not a memory address.
C is correct because `ref` is a static reference variable that is not explicitly initialized. In Java, uninitialized static reference variables default to `null`, and printing a `null` reference outputs the string "null".
Incorrect, there are no compilation errors in the provided code.
Incorrect, printing a `null` reference using `System.out.println()` does not cause a `NullPointerException`.
Concept tested: Java default values for static fields, null references.
Source: https://docs.oracle.com/javase/specs/jls/se17/html/jls-4.html#jls-4.12.5
Topics
Community Discussion
No community discussion yet for this question.