On Making Arrays
Here’s how you can convert this information into flashcards. Each card contains a concise question and answer for quick recall.
Q: How do you create a 1D array?
A: int[] array = new int[5];
Q: How do you create a 2D array?
A: int[][] matrix = new int[3][4];
Q: What is the default value of array elements?
A: 0
for numeric arrays, null
for objects.
Q: How do you initialize a 1D array with values?
A: int[] array = {1, 2, 3, 4, 5};
Q: How do you initialize a 2D array with values?
A:
int[][] matrix = {
{1, 2, 3},
{4, 5, 6}
};
Q: How do you fill a 1D array with user input?
A:
for (int i = 0; i < array.length; i++) {
array[i] = scanner.nextInt();
}
Q: How do you fill a 2D array with user input?
A:
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = scanner.nextInt();
}
}
Q: How do you iterate through a 1D array to display elements?
A:
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
Q: How do you iterate through a 2D array to display elements?
A:
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
Q: How do you sum all elements in a 1D array?
A:
int sum = 0;
for (int num : array) {
sum += num;
}
Q: How do you sum all elements in a 2D array?
A:
int sum = 0;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
sum += matrix[i][j];
}
}
Q: How do you get the length of a 1D array?
A: array.length
Q: How do you get the number of rows in a 2D array?
A: matrix.length
Q: How do you get the number of columns in a 2D array?
A: matrix[i].length
(for row i
).
Q: What is the index range for an array of size 5?
A: 0
to 4
.
Q: How do nested loops work for a 2D array?
A: Outer loop = rows, Inner loop = columns.
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
// Access element at [i][j]
}
}
Q: What is the default value for uninitialized elements in an array?
A: 0
for numbers, false
for booleans, null
for objects.
Q: Can arrays hold objects in Java?
A: Yes.
Q: What does .length
represent for an array?
A: The total number of elements.
Q: What is a ragged array?
A: A 2D array where rows have different lengths.
Here’s how you can convert this information into flashcards. Each card contains a concise question and answer for quick recall.
Q: How do you create a 1D array?
A: int[] array = new int[5];
Q: How do you create a 2D array?
A: int[][] matrix = new int[3][4];
Q: What is the default value of array elements?
A: 0
for numeric arrays, null
for objects.
Q: How do you initialize a 1D array with values?
A: int[] array = {1, 2, 3, 4, 5};
Q: How do you initialize a 2D array with values?
A:
int[][] matrix = {
{1, 2, 3},
{4, 5, 6}
};
Q: How do you fill a 1D array with user input?
A:
for (int i = 0; i < array.length; i++) {
array[i] = scanner.nextInt();
}
Q: How do you fill a 2D array with user input?
A:
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = scanner.nextInt();
}
}
Q: How do you iterate through a 1D array to display elements?
A:
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
Q: How do you iterate through a 2D array to display elements?
A:
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
Q: How do you sum all elements in a 1D array?
A:
int sum = 0;
for (int num : array) {
sum += num;
}
Q: How do you sum all elements in a 2D array?
A:
int sum = 0;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
sum += matrix[i][j];
}
}
Q: How do you get the length of a 1D array?
A: array.length
Q: How do you get the number of rows in a 2D array?
A: matrix.length
Q: How do you get the number of columns in a 2D array?
A: matrix[i].length
(for row i
).
Q: What is the index range for an array of size 5?
A: 0
to 4
.
Q: How do nested loops work for a 2D array?
A: Outer loop = rows, Inner loop = columns.
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
// Access element at [i][j]
}
}
Q: What is the default value for uninitialized elements in an array?
A: 0
for numbers, false
for booleans, null
for objects.
Q: Can arrays hold objects in Java?
A: Yes.
Q: What does .length
represent for an array?
A: The total number of elements.
Q: What is a ragged array?
A: A 2D array where rows have different lengths.