nerdexam
Oracle

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.

Working with Java Data Types

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)
  • A
    3% (1)
  • B
    3% (1)
  • C
    86% (30)
  • D
    9% (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.

Ap1.Test.class

Incorrect, `ref` is a reference to an object of type `Test`, not the `Class` object itself.

B<the summary address refrenced by ref>

Incorrect, `System.out.println()` for a null reference prints the string "null", not a memory address.

CNullCorrect

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".

DCompilation fails

Incorrect, there are no compilation errors in the provided code.

EA NullPointerException is thrown at runtime

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

#default values#static members#reference types#primitive data types

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice