A two-dimensional array in Java is like a table with rows and columns. It stores data in a grid format, where each element is accessed by two indexes: one for the row and one for the column.
Click to reveal answer
beginner
How do you declare a two-dimensional array of integers with 3 rows and 4 columns in Java?
You declare it like this: int[][] array = new int[3][4]; This creates a 3x4 grid of integers.
Click to reveal answer
beginner
How do you access the element in the second row and third column of a 2D array named 'matrix'?
You use matrix[1][2]. Remember, counting starts at 0, so the second row is index 1 and the third column is index 2.
Click to reveal answer
intermediate
How can you find the number of rows and columns in a two-dimensional array named 'data'?
The number of rows is data.length. The number of columns in the first row is data[0].length. This assumes all rows have the same number of columns.
Click to reveal answer
beginner
What happens if you try to access an index outside the bounds of a two-dimensional array in Java?
Java throws an ArrayIndexOutOfBoundsException. This means you tried to use a row or column number that does not exist in the array.
Click to reveal answer
How do you declare a 2D array of doubles with 5 rows and 2 columns in Java?
Adouble arr[][] = new double[2][5];
Bdouble[][] arr = new double[5][2];
Cdouble arr = new double[5,2];
Ddouble arr[5][2];
✗ Incorrect
Option B correctly declares a 2D array with 5 rows and 2 columns. Java uses new type[row][column] syntax.
What is the index of the first element in a two-dimensional array?
A1,0
B1,1
C0,0
D0,1
✗ Incorrect
Java arrays start at index 0, so the first element is at row 0, column 0.
If you have int[][] grid = new int[4][3];, what is grid.length?
A4
B3
C7
D12
✗ Incorrect
grid.length gives the number of rows, which is 4.
How do you access the element in the last row and last column of a 2D array named 'table'?
Atable[table.length-1][table[0].length-1]
Btable[table.length][table[0].length]
Ctable[0][0]
Dtable[table.length+1][table[0].length+1]
✗ Incorrect
Since indexes start at 0, the last element is at length-1 for both rows and columns.
What exception is thrown if you try to access an invalid index in a 2D array?
ANullPointerException
BClassCastException
CIllegalArgumentException
DArrayIndexOutOfBoundsException
✗ Incorrect
Accessing outside the array bounds throws ArrayIndexOutOfBoundsException.
Explain how to declare, initialize, and access elements in a two-dimensional array in Java.
Think of a table with rows and columns.
You got /4 concepts.
Describe how to find the size (number of rows and columns) of a two-dimensional array and why it matters.
Knowing size helps to loop safely through the array.
You got /3 concepts.
Practice
(1/5)
1. What does a two-dimensional array in Java represent?
easy
A. A collection of unrelated variables
B. A single list of elements
C. A table with rows and columns
D. A method to sort numbers
Solution
Step 1: Understand the structure of two-dimensional arrays
Two-dimensional arrays store data in rows and columns, like a grid or table.
Step 2: Compare options with the definition
Only A table with rows and columns correctly describes this as a table with rows and columns.
Final Answer:
A table with rows and columns -> Option C
Quick Check:
Two-dimensional array = table [OK]
Hint: Think of it as a spreadsheet with rows and columns [OK]
Common Mistakes:
Confusing with one-dimensional arrays
Thinking it's a list of unrelated variables
Mixing arrays with methods
2. Which of the following is the correct way to declare a two-dimensional array of integers in Java?
easy
A. int matrix[3][4];
B. int[][] matrix = new int[3][4];
C. int matrix = new int[3,4];
D. int[] matrix = new int[3][4];
Solution
Step 1: Recall Java syntax for 2D array declaration
Java uses int[][] name = new int[rows][columns]; to declare 2D arrays.
Step 2: Check each option
int[][] matrix = new int[3][4]; matches correct syntax. int matrix[3][4]; is C-style, invalid in Java. int matrix = new int[3,4]; uses comma, invalid. int[] matrix = new int[3][4]; declares a 1D array incorrectly.