What is the correct way to declare and initialize an array in Java?
1 Crore+ students have signed up on EduRev. Have you? Download the App |
Which of the following statements about the enhanced for loop in Java is correct?
What will be the output of the following code snippet?
int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers[2]);
What will be the output of the following code snippet?
int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers.length);
What will happen when you execute the following code?
int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers[10]);
What will be the output of the following code snippet?
int[] numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
System.out.print(num + " ");
}
What will be the output of the following code snippet?
int[] numbers = {1, 2, 3, 4, 5};
int[] copied = Arrays.copyOfRange(numbers, 1, 4);
System.out.println(Arrays.toString(copied));
What will be the output of the following code snippet?
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int max = matrix[0][0];
for (int[] row : matrix) {
for (int num : row) {
if (num > max) {
max = num;
}
}
}
System.out.println(max);
What will be the output of the following code snippet?
int[][] jagged = {{1, 2}, {3, 4, 5}, {6, 7, 8, 9}};
int oddCount = 0;
for (int[] row : jagged) {
for (int num : row) {
if (num % 2 != 0) {
oddCount++;
}
}
}
System.out.println(oddCount);
What will be the output of the following code snippet?
int[][][] cube = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};
int sum = 0;
for (int[][] face : cube) {
for (int[] row : face) {
for (int num : row) {
sum += num;
}
}
}
System.out.println(sum);
What will be the output of the following code snippet?
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int sum = 0;
int count = 0;
for (int[] row : matrix) {
for (int num : row) {
sum += num;
count++;
}
}
double average = (double) sum / count;
System.out.println(average);
What will be the output of the following code snippet?
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int[][] transpose = new int[matrix[0].length][matrix.length];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
transpose[j][i] = matrix[i][j];
}
}
for (int[] row : transpose) {
for (int num : row) {
System.out.print(num + " ");
}
System.out.println();
}