Ask Question
10 July, 12:11

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 (inti = 0; i < a. length; i++) result + = a[i]; System. out. printf ("Result is: %d%n", result); } }The output of this program is:a. 62b. 64c. 65d. 67

+4
Answers (1)
  1. 10 July, 12:40
    0
    c. 65

    Explanation:

    The output is 65.

    An array of length 10 is created first. Then, the first for-loop fill the array with different values; The array element now become: 2, 3, 4, 5, 6, 7, 8, 9, 10, 11. The array element are generated using the equation a[i] = i + 2; so when i is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. i must be less than the array length (10).

    a[0] = 0 + 2 = 2

    a[1] = 1 + 2 = 3

    a[2] = 2 + 2 = 4

    a[3] = 3 + 2 = 5

    a[4] = 4 + 2 = 6

    a[5] = 5 + 2 = 7

    a[6] = 6 + 2 = 8

    a[7] = 7 + 2 = 9

    a[8] = 8 + 2 = 10

    a[9] = 9 + 2 = 11

    result variable is declared and initialized to 0.

    The second for-loop goes through the array and add individual element to result.
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 < a. ...” in 📗 Physics 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