In Java, which option both declares an int array and initializes it immediately with five specific numbers?

Difficulty: Easy

Correct Answer: int [] a = {23, 22, 21, 20, 19};

Explanation:


Introduction / Context:
Arrays in Java are objects with fixed length. You can declare and initialize them in one statement using an initializer list, which is common for known small datasets.



Given Data / Assumptions:

  • We need both declaration and immediate initialization with five numbers.
  • Java syntax, not C/C++ custom array syntax, applies.


Concept / Approach:
Use the brace form: type[] name = {v1, v2, ...}. This creates the array object and fills it with the specified values. The length is inferred from the list.



Step-by-Step Solution:

Option B uses the correct brace initializer → valid.Option A references a non-existent Array type constructor in core Java → invalid.Option C only allocates length 5 with default zeros; it does not initialize with the given numbers.Option D uses illegal Java syntax (size in brackets on the left).


Verification / Alternative check:
Print a[0..4] after option B; it shows the listed values, not zeros.



Why Other Options Are Wrong:
They either lack initialization values or use invalid types/syntax.



Common Pitfalls:
Confusing Java with C-style declarations or assuming Array is a built-in type to construct directly.



Final Answer:
int [] a = {23, 22, 21, 20, 19};

More Questions from Language Fundamentals

Discussion & Comments

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