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

Discussion :: Declarations and Access Control

  1. Which one creates an instance of an array?

  2. A.
    int[ ] ia = new int[15];
    B.
    float fa = new float[20];
    C.
    char[ ] ca = "Some String";
    D.
    int ia[ ] [ ] = { 4, 5, 6 }, { 1,2,3 };

    View Answer

    Workspace

    Answer : Option A

    Explanation :

    Option A is correct. It uses correct array declaration and correct array construction.

    Option B is incorrect. It generates a compiler error: incompatible types because the array variable declaration is not correct. The array construction expects a reference type, but it is supplied with a primitive type in the declaration.

    Option C is incorrect. It generates a compiler error: incompatible types because a string literal is not assignable to a character type variable.

    Option D is wrong, it generates a compiler error <identifier> expected. The compiler thinks that 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 a 3 x 3 two-dimensional array.


Be The First To Comment