A

Admin • 828.03K Points
Coach

Q. What is the output of the following code?

Code:
int[] arr = new int[]{1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
System.out.println(sum);
  • (A) 15
  • (B) 10
  • (C) 6
  • (D) 3
  • Correct Answer - Option(A)
  • Views: 15
  • Filed under category Java
  • Hashtags:

Explanation by: Admin
The for loop iterates over each element of the array and adds its value to the variable sum, which is then printed.

You must be Logged in to update hint/solution

Discusssion

Login to discuss.

Be the first to start discuss.