Home / Java Programming / Declarations and Access Control :: Discussion

Discussion :: Declarations and Access Control

  1. Which cause a compiler error?

  2. A.
    int[ ] scores = {3, 5, 7};
    B.
    int [ ][ ] scores = {2,7,6}, {9,3,45};
    C.
    String cats[ ] = {"Fluffy", "Spot", "Zeus"};
    D.
    boolean results[ ] = new boolean [] {true, false, true};
    E.
    Integer results[ ] = {new Integer(3), new Integer(5), new Integer(8)};

    View Answer

    Workspace

    Answer : Option B

    Explanation :

    Option B generates a compiler error: <identifier> expected. The compiler thinks you are trying to create two arrays because there are two array initialisers to the right of the equals, whereas your intention was to create one 3 x 3 two-dimensional array.

    To correct the problem and make option B compile you need to add an extra pair of curly brackets:

    int [ ] [ ] scores = { {2,7,6}, {9,3,45} };


Be The First To Comment