Home / Java Programming / Language Fundamentals :: Discussion

Discussion :: Language Fundamentals

  1. Which three are legal array declarations?

    1. int [] myScores [];
    2. char [] myChars;
    3. int [6] myScores;
    4. Dog myDogs [];
    5. Dog myDogs [7];

  2. A.
    1, 2, 4
    B.
    2, 4, 5
    C.
    2, 3, 4
    D.
    All are correct.

    View Answer

    Workspace

    Answer : Option A

    Explanation :

    (1), (2), and (4) are legal array declarations. With an array declaration, you can place the brackets to the right or left of the identifier. Option A looks strange, but it's perfectly legal to split the brackets in a multidimensional array, and place them on both sides of the identifier. Although coding this way would only annoy your fellow programmers, for the exam, you need to know it's legal.

    (3) and (5) are wrong because you can't declare an array with a size. The size is only needed when the array is actually instantiated (and the JVM needs to know how much space to allocate for the array, based on the type of array and the size).


Be The First To Comment