A

Admin • 828.70K Points
Coach

Q. What is the output of the following code?

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

Explanation by: Admin
This for loop iterates through the elements of the nums array, using the index variable i, and adds each element to the variable sum. The System.out.println statement then prints out the final value of sum, which is 15.

You must be Logged in to update hint/solution

Discusssion

Login to discuss.

Be the first to start discuss.