All Flashcards
What does the following code output?
java
int[][] arrayB = {{1, 2}, {3, 4}};
for (int i = 0; i < arrayB.length; i++) {
for (int j = 0; j < arrayB[0].length; j++) {
System.out.print(arrayB[i][j] + " ");
}
}
1 2 3 4
What does the following code output?
java
int[][] arrayB = {{1, 2}, {3, 4}};
for (int i = 0; i < arrayB[0].length; i++) {
for (int j = 0; j < arrayB.length; j++) {
System.out.print(arrayB[j][i] + " ");
}
}
1 3 2 4
Identify the error in the following code:
java
public static void main(str[] args) {
int[][] arrayB = {{1, 2}, {3, 4}};
rowWiseForward(arrayB);
}
The parameter in main should be String[], not str[].
What does the following code output?
java
int[][] arrayB = {{1, 2}, {3, 4}};
int sum = 0;
for (int[] row : arrayB) {
for (int num : row) {
sum += num;
}
}
System.out.println(sum);
10
What does the following code output?
java
int[][] arrayB = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
System.out.println(arrayB.length);
3
What does the following code output?
java
int[][] arrayB = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
System.out.println(arrayB[0].length);
4
What does the following code output?
java
int[][] arrayB = {{1, 2}, {3, 4}};
System.out.println(arrayB[1][0]);
3
What does the following code output?
java
int[][] arrayB = {{1, 2}, {3, 4}};
int product = 1;
for (int i = 0; i < arrayB.length; i++) {
for (int j = 0; j < arrayB[0].length; j++) {
product *= arrayB[i][j];
}
}
System.out.println(product);
24
What does the following code output?
java
int[][] arrayB = {{1, 2}, {3, 4}};
int max = arrayB[0][0];
for (int i = 0; i < arrayB.length; i++) {
for (int j = 0; j < arrayB[0].length; j++) {
if (arrayB[i][j] > max) {
max = arrayB[i][j];
}
}
}
System.out.println(max);
4
What does the following code output?
java
int[][] arrayB = {{1, 2, 3}, {4, 5, 6}};
int count = 0;
for (int[] row : arrayB) {
for (int num : row) {
if (num % 2 == 0) {
count++;
}
}
}
System.out.println(count);
3
What are the differences between row-major and column-major traversal?
Row-major: Processes rows sequentially. | Column-major: Processes columns sequentially.
Compare regular for loops and enhanced for loops for modifying 2D array elements.
Regular for loops: Allow direct modification of array elements. | Enhanced for loops: Operate on copies, so modifications don't affect the original array (unless object instance variables are modified).
Why are nested loops needed for 2D array traversal?
One loop iterates through the rows, and the other iterates through the columns in each row.
Explain the difference between regular and enhanced for loops when modifying 2D array elements.
Regular for loops access the array directly, allowing modification. Enhanced for loops operate on copies, so modifications don't affect the original array (unless object instance variables are modified).
Why initialize maxValue to array[0][0] instead of 0 when finding the maximum value?
If all array values are negative, initializing to 0 would incorrectly identify 0 as the maximum.
Why can enhanced for loops be used when changing the value of an object's instance variable?
The copy contains references to the actual objects themselves.
What is the significance of 'i < array.length' in 2D array traversal?
It ensures the loop iterates through all rows of the array.
What is the significance of 'i < array[0].length' in 2D array traversal?
It ensures the loop iterates through all columns of the array.
How does the modulo operator help in snaking around?
The modulo operator helps alternate the direction of traversal every row or column.
What is the first step when finding the mode of an array?
Place the numbers into a 1D array.
What is the formula for calculating the mean?
Sum of all elements / (array.length * array[0].length)
What is the difference between finding a sum and finding a mean?
Finding the mean requires one more step, dividing the sum by the total number of elements.