unit 8 Flashcards

(25 cards)

1
Q

What is a 2D array in Java?

A

An array of arrays; a table with rows and columns.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you declare a 2D array?

A

dataType[][] arrayName;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you create a 2D int array with 3 rows and 4 columns?

A

new int[3][4];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you access an element in a 2D array?

A

arrayName[row][col];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you set an element in a 2D array?

A

arrayName[row][col] = value;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does array.length return for a 2D array?

A

The number of rows.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you find the number of columns in a 2D array?

A

arrayName[row].length

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Can each row of a 2D array have a different length?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you traverse a 2D array?

A

Nested for loops: outer loop for rows

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Write a basic 2D array traversal.

A

for (int r = 0; r < arr.length; r++) { for (int c = 0; c < arr[r].length; c++) { arr[r][c]; }}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is row-major order traversal?

A

Visiting all elements row by row (row first

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is column-major order traversal?

A

Visiting all elements column by column (column first

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you initialize a 2D array with values?

A

int[][] arr = {{1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What happens if you access an invalid index in a 2D array?

A

ArrayIndexOutOfBoundsException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you sum all elements in a 2D array?

A

Use nested loops and add each element.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you find the maximum value in a 2D array?

A

Initialize max

17
Q

What is a practical use of 2D arrays?

A

Storing grids

18
Q

What is the difference between arr.length and arr[0].length?

A

arr.length is number of rows

19
Q

How can you print a 2D array neatly?

A

Use nested loops with System.out.print and newline at end of each row.

20
Q

How can you reverse rows in a 2D array?

A

Swap elements in each row moving inward.

21
Q

What should you always check before accessing arr[r][c]?

A

Ensure 0 <= r < arr.length and 0 <= c < arr[r].length

22
Q

How do you count a certain value in a 2D array?

A

Use nested loops and an if-statement.

23
Q

What happens when you assign one 2D array to another?

A

They both reference the same array object (aliasing).

24
Q

How do you create a deep copy of a 2D array?

A

Create a new array and copy each row manually.

25
What does arr[0][0] access?
The element in the first row and first column.