Which statement legally declares, constructs, and initializes a one-dimensional int array in Java?

Java Programming Language Fundamentals Difficulty: Easy
Choose an option
  • A
    int [] myList = {'1', '2', '3'};
  • B
    int [] myList = (5, 8, 2);
  • C
    int myList [] [] = {4, 9, 7, 0};
  • D
    int myList [] = {4, 3, 7};
  • E
    None of these

Answer

Correct Answer: int myList [] = {4, 3, 7};

Explanation

Introduction / Context:Array initialization in Java can be done succinctly using an initializer list, which both constructs and fills the array. The syntax must match the array type and dimensionality.

Given Data / Assumptions:

  • We are creating an int[] and immediately providing values.

Concept / Approach:The correct pattern is: type[] name = { v1, v2, ... };. Characters in single quotes are chars, not ints (though chars can convert to int, the example uses the wrong literal intent). Tuple-like parentheses are not Java syntax. Multi-dimensional declarations must match initializer shapes.

Step-by-Step Solution:

Option D → int myList [] = {4, 3, 7}; correct declaration and initialization.Option A → uses char literals '1', etc.; even if convertible, it does not express integer numeric literals as intended.Option B → invalid tuple syntax in Java.Option C → declares a 2D array but provides a 1D initializer → mismatch.

Verification / Alternative check:Print myList.length and elements after Option D; results reflect the three specified ints.

Why Other Options Are Wrong:They use invalid syntax or mismatched dimensionality/literal types.

Common Pitfalls:Using parentheses instead of braces; confusing char and int literals; mismatching array dimension with initializer structure.

Final Answer:int myList [] = {4, 3, 7};

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