public static void main(String args[]){ int array_variable[ ][ ] = {{1,2,3},{4,5,6},{7,8,9}}; int sum = 0; for ( int i = 0; i < 3; ++i) for( int j = 0; j < 3 ; ++j) sum = sum + array_variable[i][j]; System.out.print(sum / 5); }}
Question
public static void main(String args[]){ int array_variable[ ][ ] = {{1,2,3},{4,5,6},{7,8,9}}; int sum = 0; for ( int i = 0; i < 3; ++i) for( int j = 0; j < 3 ; ++j) sum = sum + array_variable[i][j]; System.out.print(sum / 5); }}
Solution
This Java code is creating a 2D array, summing all the elements in the array, and then printing the result of the sum divided by 5.
Here's a step-by-step breakdown:
-
A 2D array named
array_variableis declared and initialized with the values {{1,2,3},{4,5,6},{7,8,9}}. -
An integer variable
sumis declared and initialized with the value 0. -
A nested for loop is used to iterate over the elements in the 2D array. The outer loop iterates over the rows and the inner loop iterates over the columns.
-
Inside the inner loop, the value of the current element is added to
sum. -
After all elements have been added to
sum, the value ofsumis divided by 5. -
The result of
sum / 5is printed to the console.
The output of this code will be the sum of all the numbers in the array (which is 45) divided by 5, so the output will be 9.
Similar Questions
Select the correct answerWhat will be the output of the following Java code?class array_output { public static void main(String args[]) { int array_variable[][] = {{ 1, 2, 3}, { 4 , 5, 6}, { 7, 8, 9}}; int sum = 0; for (int i = 1; i < 3; ++i) for (int j = 1; j < 3 ; ++j) sum = sum + array_variable[i][j]; System.out.print(sum / 6); } }Options8413
int [ ] array = { 2, 4, 6, 8, 1, 3, 5, 7}; int george = 0; for (int i = 0; i < array.length; i++) george += array[i]; System.out.print(george);
What will be the output of the below program?public class Demo { public static void main(String[] args) { int sum = 0; for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1) { sum += i; } System.out.println(sum); }}
Java program to sum values of an array.Write a Java program to sum values of an array. Constraints:N/AExample:Input:1, 2, 3, 4, 5, 6, 7, 8, 9, 10Output:55Explanation:-Public Test Cases:# INPUT EXPECTED OUTPUT1 1, 2, 3, 4, 5, 6, 7, 8, 9, 1055
Select the correct answerWhat will be the output of the following Java code?class multidimention_array { public static void main(String args[]) { int arr[][] = new int[3][]; arr[0] = new int[1]; arr[1] = new int[2]; arr[2] = new int[3]; int sum = 0; for (int ct1 = 0; ct1 < 3; ++ct1) for (int ct2 = 0; ct2 < ct1 + 1; ++ct2) arr[ct1][ct2] = ct2 + 1; for (int ct1 = 0; ct1 < 3; ++ct1) for (int ct2 = 0; ct2 < ct1 + 1; ++ct2) sum += arr[ct1][ct2]; System.out.print(sum); } }Options13101114
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.