1

I was going through one App which is suggested for the OCJP exam. I found one question about the two-dimensional array.

Question: Valid declarations of a two-dimensional array.

Options:

  1. int[][] array2D;
  2. int[2][2] array2D;
  3. int array2D[];
  4. int[] array2D[];
  5. int[][] array2D[];

My selection: int[][] array2D; and int[] array2D[] but when I submit my answer it tells me that int[] array2D[] is wrong and correct is int[][] array2D[];

I think the int[][] array2D[]; is incorrect answer.

  1. Am I right?
  2. Is int[] array2D[] recommended in programming?
10
  • 1
    Your question was so poorly formatted, please do some effort next time. Commented Feb 9, 2017 at 6:56
  • 2
    1) You are correct. 2) int[] array2D[] is discouraged by the Java Language Specification itself: We do not recommend "mixed notation" in array variable declarations, where bracket pairs appear on both the type and in declarators; nor in method declarations, where bracket pairs appear both before and after the formal parameter list. Commented Feb 9, 2017 at 6:56
  • how the hell 5th option is correct ? Commented Feb 9, 2017 at 6:57
  • 3th Option is not even 2D array. It can have only one set. Commented Feb 9, 2017 at 7:01
  • 1
    JVM wouldn't give you error for this, JVM doesn't know you want it to be treated as 2d array. Commented Feb 9, 2017 at 7:09

1 Answer 1

5

The answer to this seems to be both options 1 and 4.

int[][] array2D 

is a standard way to declare a 2-d int array.

Although int[] array2D[]; it is not a good practice it will work and will be a valid declaration.

Sign up to request clarification or add additional context in comments.

3 Comments

typo in last statement ? int [] array2D [] perhaps ?
"not a good practice" is called "we do not recommend" in the JLS. See my comment.
@Akshay agreed with your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.