Which of the following Java statements declare and allocate a 2-dimensional…

2017

Which of the following Java statements declare and allocate a 2-dimensional array of integers with four rows and five columns?

Answer: D. int array[][] = new int[4][5];Key idea: In Java, array sizes are specified when creating the array object with new; you cannot put sizes in the declaration. To create a 2D int array with…

  1. A.

    int array[][] = new int[5][4];

  2. B.

    int array[4][5];

  3. C.

    int array[5][4];

  4. D.

    int array[][] = new int[4][5];

Attempted by 489 students.

Show answer & explanation

Correct answer: D

Key idea: In Java, array sizes are specified when creating the array object with new; you cannot put sizes in the declaration.

  • To create a 2D int array with four rows and five columns use: int[][] array = new int[4][5]; or int array[][] = new int[4][5];

  • The first dimension is the number of rows and the second is the number of columns. Valid row indices: 0 to 3. Valid column indices: 0 to 4.

  • Declarations that include sizes like int array[4][5]; are not valid Java syntax. You must use new when providing sizes.

  • An expression new int[5][4] would allocate 5 rows and 4 columns, which is not what this question asks for.

Final answer: int array[][] = new int[4][5]; — this correctly declares and allocates a 2D int array with four rows and five columns.

संक्षेप में: जावा में आकार new के साथ दिया जाता है; चार पंक्तियाँ और पाँच स्तम्भ बनाने के लिए int[][] array = new int[4][5]; का प्रयोग करें।

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Rssb Basic Computer Instructor

Loading lesson…