Ask Question
19 August, 01:50

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:

+5
Answers (1)
  1. 19 August, 02:07
    0
    Result is: 65

    Explanation:

    To analyze this code let us asign line numbers:

    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); } }

    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)
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Consider the program below: public class Test { public static void main (String[] args) { int[] a; a = new int[ 10 ]; for (int i = 0; i < ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers