Java arrays default values: which four entries correctly describe default element values for the given types?

Difficulty: Easy

Correct Answer: 1, 3, 4, 5

Explanation:

Introduction / Context:When you create an array in Java, every element is automatically initialized to a type-specific default. This behavior differs for primitives and reference types.

Given Data / Assumptions:

  • Types and proposed defaults: int → 0, String → ""null"" (string literal), Dog → null, char → '\u0000', float → 0.0f, boolean → true.

Concept / Approach:Java default element values: numeric primitives → 0/0.0, char → '\u0000', boolean → false, reference types → null (not the string ""null"").

Step-by-Step Solution:

int → 0 → correct.String → ""null"" → incorrect (should be null reference).Dog (reference) → null → correct.char → '\u0000' → correct (NUL code unit 0).float → 0.0f → correct.boolean → true → incorrect (default is false).

Verification / Alternative check:Allocate new arrays and print default values; booleans print false, references print null.

Why Other Options Are Wrong:Any set that includes String → ""null"" or boolean → true is incorrect.

Common Pitfalls:Misunderstanding null vs. the string ""null""; assuming booleans default to true.

Final Answer:1, 3, 4, 5

More Questions from Language Fundamentals

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion