Consider the program below: public class Test { public static void main( String[] args ) { int[] a; a = new int[ 10 ]; for ( int i = 0; i < a.length; i++ ) a[ i ] = i + 2; int result = 0; for ( int i = 0; i < a.length; i++ ) result += a[ i ]; System.out.printf( "Result is: %d\n", result ); } // end main } // end class Test The output of this program will be:

Respuesta :

ijeggs

Answer:

Result is: 65

Explanation:

To analyze this code let us asign line numbers:

  1. public class Test {
  2.    public static void main( String[] args )
  3.    { int[] a; a = new int[ 10 ];
  4.        for ( int i = 0; i < a.length; i++ )
  5.            a[ i ] = i + 2;
  6.        int result = 0;
  7.        for ( int i = 0; i < a.length; i++ )
  8.            result += a[ i ];
  9.        System.out.printf( "Result is: %d\n", result );
  10.    }
  11. }

On line 3, an array of ints is created of size 10

On line 4 and 5 a for loop is used to add values to the array. at the end of the execution of line 4 and 5, the array will contain the following elements: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

On line 6 an int variable result is created and initialized to 0

line 7 and 8 allows a for loop through the array adding up all the elements of the array and assigning them to result

Line 9 outputs the value of result (Which is the total sum)